1- <?php
1+ <?php
22
33namespace Primal \Color ;
44
@@ -7,28 +7,28 @@ class RGBColor extends Color {
77 public $ green = 0 ;
88 public $ blue = 0 ;
99 public $ alpha = 1 ;
10-
10+
1111 function __construct ($ r = 0 , $ g = 0 , $ b = 0 , $ a =1 ) {
1212 $ this ->red = $ r ;
1313 $ this ->green = $ g ;
1414 $ this ->blue = $ b ;
1515 $ this ->alpha = $ a ;
1616 }
17-
17+
1818 function toHSV () {
1919 $ r = ((int )$ this ->red % 256 ) / 255 ;
2020 $ g = ((int )$ this ->green % 256 ) / 255 ;
2121 $ b = ((int )$ this ->blue % 256 ) / 255 ;
2222 $ a = $ this ->alpha ;
23-
23+
2424 $ max = max ($ r , $ g , $ b );
2525 $ min = min ($ r , $ g , $ b );
2626 $ d = $ max - $ min ;
2727
2828 $ h = 0 ;
2929 $ s = ($ max === 0 ) ? 0 : $ d / $ max ;
3030 $ v = $ max ;
31-
31+
3232 if ($ max !== $ min ) {
3333 switch ($ max ) {
3434 case $ r :
@@ -42,19 +42,19 @@ function toHSV() {
4242 }
4343 $ h = $ h / 6 ;
4444 }
45-
45+
4646 return new HSVColor ($ h *360 , $ s *100 , $ v *100 , $ a );
4747 }
48-
48+
4949 function toHSL () {
5050 $ r = ((int )$ this ->red % 256 ) / 255 ;
5151 $ g = ((int )$ this ->green % 256 ) / 255 ;
5252 $ b = ((int )$ this ->blue % 256 ) / 255 ;
5353 $ a = $ this ->alpha ;
54-
54+
5555 $ max = max ($ r , $ g , $ b );
5656 $ min = min ($ r , $ g , $ b );
57-
57+
5858 $ l = ($ max + $ min ) / 2 ;
5959 if ($ max === $ min ) {
6060 $ h = $ s = 0 ;
@@ -73,10 +73,10 @@ function toHSL() {
7373 }
7474 $ h = $ h / 6 ;
7575 }
76-
76+
7777 return new HSLColor ($ h *360 , $ s *100 , $ l *100 , $ a );
7878 }
79-
79+
8080 function toCMYK () {
8181 $ r = ((int )$ this ->red % 256 ) / 255 ;
8282 $ g = ((int )$ this ->green % 256 ) / 255 ;
@@ -91,26 +91,28 @@ function toCMYK() {
9191 $ c = (1 - $ r - $ k ) / (1 - $ k );
9292 $ m = (1 - $ g - $ k ) / (1 - $ k );
9393 $ y = (1 - $ b - $ k ) / (1 - $ k );
94-
94+
9595 return new CMYKColor ($ c *100 , $ m *100 , $ y *100 , $ k *100 , $ a );
9696 }
97-
97+
9898 function toRGB () {
9999 return clone $ this ;
100100 }
101-
101+
102102 function toCSS ($ alpha = null ) {
103- return (($ alpha === true || $ this ->alpha < 1 ) && $ alpha !== false ) ? "rgba( {$ this ->red }, {$ this ->green }, {$ this ->blue }, {$ this ->alpha }) " : "rgb( {$ this ->red }, {$ this ->green }, {$ this ->blue }) " ;
103+ return ($ alpha === true || $ this ->alpha < 1 ) && $ alpha !== false
104+ ? sprintf ('rgba(%d, %d, %d, %s) ' , $ this ->red , $ this ->green , $ this ->blue , $ this ->alpha )
105+ : sprintf ('rgb(%d, %d, %d) ' , $ this ->red , $ this ->green , $ this ->blue );
104106 }
105-
107+
106108 function toHex () {
107109 $ stack = array ('# ' );
108110 $ stack [] = str_pad (dechex (min (255 , round ($ this ->red ))), 2 , '0 ' , STR_PAD_LEFT );
109111 $ stack [] = str_pad (dechex (min (255 , round ($ this ->green ))), 2 , '0 ' , STR_PAD_LEFT );
110112 $ stack [] = str_pad (dechex (min (255 , round ($ this ->blue ))), 2 , '0 ' , STR_PAD_LEFT );
111-
113+
112114 return implode ('' , $ stack );
113115 }
114-
116+
115117}
116118
0 commit comments