@@ -42,7 +42,7 @@ private static function _number( $n ) {
4242 }
4343 }
4444
45- private static function _scaled ( $ n , $ size = 255 ) {
45+ private static function _scaled ( $ n , $ size ) {
4646 if ( $ n instanceof Less_Tree_Dimension && $ n ->unit ->is ( '% ' ) ) {
4747 return (float )$ n ->value * $ size / 100 ;
4848 } else {
@@ -51,9 +51,6 @@ private static function _scaled( $n, $size = 255 ) {
5151 }
5252
5353 public function rgb ( $ r = null , $ g = null , $ b = null ) {
54- if ( $ r === null || $ g === null || $ b === null ) {
55- throw new Less_Exception_Compiler ( "rgb expects three parameters " );
56- }
5754 $ color = $ this ->rgba ( $ r , $ g , $ b , 1.0 );
5855 if ( $ color ) {
5956 $ color ->value = 'rgb ' ;
@@ -62,14 +59,26 @@ public function rgb( $r = null, $g = null, $b = null ) {
6259 }
6360
6461 public function rgba ( $ r = null , $ g = null , $ b = null , $ a = null ) {
65- $ rgb = [
66- self ::_scaled ( $ r ),
67- self ::_scaled ( $ g ),
68- self ::_scaled ( $ b )
69- ];
62+ try {
63+ if ( $ r instanceof Less_Tree_Color ) {
64+ if ( $ g ) {
65+ $ a = self ::_number ( $ g );
66+ } else {
67+ $ a = $ r ->alpha ;
68+ }
69+ return new Less_Tree_Color ( $ r ->rgb , $ a , 'rgba ' );
70+ }
71+ $ rgb = [
72+ self ::_scaled ( $ r , 255 ),
73+ self ::_scaled ( $ g , 255 ),
74+ self ::_scaled ( $ b , 255 )
75+ ];
7076
71- $ a = self ::_number ( $ a );
72- return new Less_Tree_Color ( $ rgb , $ a , 'rgba ' );
77+ $ a = self ::_number ( $ a );
78+ return new Less_Tree_Color ( $ rgb , $ a , 'rgba ' );
79+ } catch ( Exception $ e ) {
80+
81+ }
7382 }
7483
7584 public function hsl ( $ h , $ s , $ l ) {
@@ -80,23 +89,36 @@ public function hsl( $h, $s, $l ) {
8089 }
8190 }
8291
83- public function hsla ( $ h , $ s , $ l , $ a ) {
84- $ h = fmod ( self ::_number ( $ h ), 360 ) / 360 ; // Classic % operator will change float to int
85- $ s = self ::_clamp ( self ::_number ( $ s ) );
86- $ l = self ::_clamp ( self ::_number ( $ l ) );
87- $ a = self ::_clamp ( self ::_number ( $ a ) );
92+ public function hsla ( $ h = null , $ s = null , $ l = null , $ a = null ) {
93+ try {
94+ if ( $ h instanceof Less_Tree_Color ) {
95+ if ( $ s ) {
96+ $ a = self ::_number ( $ s );
97+ } else {
98+ $ a = $ h ->alpha ;
99+ }
100+ return new Less_Tree_Color ( $ h ->rgb , $ a , 'hsla ' );
101+ }
102+ // NOTE: Avoid % operator which would change float to int
103+ $ h = fmod ( self ::_number ( $ h ), 360 ) / 360 ;
104+ $ s = self ::_clamp ( self ::_number ( $ s ) );
105+ $ l = self ::_clamp ( self ::_number ( $ l ) );
106+ $ a = self ::_clamp ( self ::_number ( $ a ) );
88107
89- $ m2 = $ l <= 0.5 ? $ l * ( $ s + 1 ) : $ l + $ s - $ l * $ s ;
108+ $ m2 = $ l <= 0.5 ? $ l * ( $ s + 1 ) : $ l + $ s - $ l * $ s ;
90109
91- $ m1 = $ l * 2 - $ m2 ;
110+ $ m1 = $ l * 2 - $ m2 ;
92111
93- $ rgb = [
94- self ::hsla_hue ( $ h + 1 / 3 , $ m1 , $ m2 ) * 255 ,
95- self ::hsla_hue ( $ h , $ m1 , $ m2 ) * 255 ,
96- self ::hsla_hue ( $ h - 1 / 3 , $ m1 , $ m2 ) * 255 ,
97- ];
98- $ a = self ::_number ( $ a );
99- return new Less_Tree_Color ( $ rgb , $ a , 'hsla ' );
112+ $ rgb = [
113+ self ::hsla_hue ( $ h + 1 / 3 , $ m1 , $ m2 ) * 255 ,
114+ self ::hsla_hue ( $ h , $ m1 , $ m2 ) * 255 ,
115+ self ::hsla_hue ( $ h - 1 / 3 , $ m1 , $ m2 ) * 255 ,
116+ ];
117+ $ a = self ::_number ( $ a );
118+ return new Less_Tree_Color ( $ rgb , $ a , 'hsla ' );
119+ } catch ( Exception $ e ) {
120+
121+ }
100122 }
101123
102124 /**
0 commit comments