16
16
class Color extends CSSFunction
17
17
{
18
18
/**
19
- * @param array<int, Value|string> $aColor
20
- * @param int $iLineNo
19
+ * @param array<int, Value|string> $colorValues
20
+ * @param int $lineNumber
21
21
*/
22
- public function __construct (array $ aColor , $ iLineNo = 0 )
22
+ public function __construct (array $ colorValues , $ lineNumber = 0 )
23
23
{
24
- parent ::__construct (\implode ('' , \array_keys ($ aColor )), $ aColor , ', ' , $ iLineNo );
24
+ parent ::__construct (\implode ('' , \array_keys ($ colorValues )), $ colorValues , ', ' , $ lineNumber );
25
25
}
26
26
27
27
/**
28
28
* @throws UnexpectedEOFException
29
29
* @throws UnexpectedTokenException
30
30
*/
31
- public static function parse (ParserState $ oParserState , bool $ bIgnoreCase = false ): CSSFunction
31
+ public static function parse (ParserState $ parserState , bool $ ignoreCase = false ): CSSFunction
32
32
{
33
33
return
34
- $ oParserState ->comes ('# ' )
35
- ? self ::parseHexColor ($ oParserState )
36
- : self ::parseColorFunction ($ oParserState );
34
+ $ parserState ->comes ('# ' )
35
+ ? self ::parseHexColor ($ parserState )
36
+ : self ::parseColorFunction ($ parserState );
37
37
}
38
38
39
39
/**
40
40
* @throws UnexpectedEOFException
41
41
* @throws UnexpectedTokenException
42
42
*/
43
- private static function parseHexColor (ParserState $ oParserState ): CSSFunction
43
+ private static function parseHexColor (ParserState $ parserState ): CSSFunction
44
44
{
45
- $ oParserState ->consume ('# ' );
46
- $ sValue = $ oParserState ->parseIdentifier (false );
47
- if ($ oParserState ->strlen ($ sValue ) === 3 ) {
48
- $ sValue = $ sValue [0 ] . $ sValue [0 ] . $ sValue [1 ] . $ sValue [1 ] . $ sValue [2 ] . $ sValue [2 ];
49
- } elseif ($ oParserState ->strlen ($ sValue ) === 4 ) {
50
- $ sValue = $ sValue [0 ] . $ sValue [0 ] . $ sValue [1 ] . $ sValue [1 ] . $ sValue [2 ] . $ sValue [ 2 ] . $ sValue [ 3 ]
51
- . $ sValue [3 ];
45
+ $ parserState ->consume ('# ' );
46
+ $ hexValue = $ parserState ->parseIdentifier (false );
47
+ if ($ parserState ->strlen ($ hexValue ) === 3 ) {
48
+ $ hexValue = $ hexValue [0 ] . $ hexValue [0 ] . $ hexValue [1 ] . $ hexValue [1 ] . $ hexValue [2 ] . $ hexValue [2 ];
49
+ } elseif ($ parserState ->strlen ($ hexValue ) === 4 ) {
50
+ $ hexValue = $ hexValue [0 ] . $ hexValue [0 ] . $ hexValue [1 ] . $ hexValue [1 ] . $ hexValue [2 ] . $ hexValue [ 2 ]
51
+ . $ hexValue [ 3 ] . $ hexValue [3 ];
52
52
}
53
53
54
- if ($ oParserState ->strlen ($ sValue ) === 8 ) {
55
- $ aColor = [
56
- 'r ' => new Size (\intval ($ sValue [0 ] . $ sValue [1 ], 16 ), null , true , $ oParserState ->currentLine ()),
57
- 'g ' => new Size (\intval ($ sValue [2 ] . $ sValue [3 ], 16 ), null , true , $ oParserState ->currentLine ()),
58
- 'b ' => new Size (\intval ($ sValue [4 ] . $ sValue [5 ], 16 ), null , true , $ oParserState ->currentLine ()),
54
+ if ($ parserState ->strlen ($ hexValue ) === 8 ) {
55
+ $ colorValues = [
56
+ 'r ' => new Size (\intval ($ hexValue [0 ] . $ hexValue [1 ], 16 ), null , true , $ parserState ->currentLine ()),
57
+ 'g ' => new Size (\intval ($ hexValue [2 ] . $ hexValue [3 ], 16 ), null , true , $ parserState ->currentLine ()),
58
+ 'b ' => new Size (\intval ($ hexValue [4 ] . $ hexValue [5 ], 16 ), null , true , $ parserState ->currentLine ()),
59
59
'a ' => new Size (
60
- \round (self ::mapRange (\intval ($ sValue [6 ] . $ sValue [7 ], 16 ), 0 , 255 , 0 , 1 ), 2 ),
60
+ \round (self ::mapRange (\intval ($ hexValue [6 ] . $ hexValue [7 ], 16 ), 0 , 255 , 0 , 1 ), 2 ),
61
61
null ,
62
62
true ,
63
- $ oParserState ->currentLine ()
63
+ $ parserState ->currentLine ()
64
64
),
65
65
];
66
- } elseif ($ oParserState ->strlen ($ sValue ) === 6 ) {
67
- $ aColor = [
68
- 'r ' => new Size (\intval ($ sValue [0 ] . $ sValue [1 ], 16 ), null , true , $ oParserState ->currentLine ()),
69
- 'g ' => new Size (\intval ($ sValue [2 ] . $ sValue [3 ], 16 ), null , true , $ oParserState ->currentLine ()),
70
- 'b ' => new Size (\intval ($ sValue [4 ] . $ sValue [5 ], 16 ), null , true , $ oParserState ->currentLine ()),
66
+ } elseif ($ parserState ->strlen ($ hexValue ) === 6 ) {
67
+ $ colorValues = [
68
+ 'r ' => new Size (\intval ($ hexValue [0 ] . $ hexValue [1 ], 16 ), null , true , $ parserState ->currentLine ()),
69
+ 'g ' => new Size (\intval ($ hexValue [2 ] . $ hexValue [3 ], 16 ), null , true , $ parserState ->currentLine ()),
70
+ 'b ' => new Size (\intval ($ hexValue [4 ] . $ hexValue [5 ], 16 ), null , true , $ parserState ->currentLine ()),
71
71
];
72
72
} else {
73
73
throw new UnexpectedTokenException (
74
74
'Invalid hex color value ' ,
75
- $ sValue ,
75
+ $ hexValue ,
76
76
'custom ' ,
77
- $ oParserState ->currentLine ()
77
+ $ parserState ->currentLine ()
78
78
);
79
79
}
80
80
81
- return new Color ($ aColor , $ oParserState ->currentLine ());
81
+ return new Color ($ colorValues , $ parserState ->currentLine ());
82
82
}
83
83
84
84
/**
85
85
* @throws UnexpectedEOFException
86
86
* @throws UnexpectedTokenException
87
87
*/
88
- private static function parseColorFunction (ParserState $ oParserState ): CSSFunction
88
+ private static function parseColorFunction (ParserState $ parserState ): CSSFunction
89
89
{
90
- $ aColor = [];
90
+ $ colorValues = [];
91
91
92
- $ sColorMode = $ oParserState ->parseIdentifier (true );
93
- $ oParserState ->consumeWhiteSpace ();
94
- $ oParserState ->consume ('( ' );
92
+ $ colorMode = $ parserState ->parseIdentifier (true );
93
+ $ parserState ->consumeWhiteSpace ();
94
+ $ parserState ->consume ('( ' );
95
95
96
96
// CSS Color Module Level 4 says that `rgb` and `rgba` are now aliases; likewise `hsl` and `hsla`.
97
97
// So, attempt to parse with the `a`, and allow for it not being there.
98
- switch ($ sColorMode ) {
98
+ switch ($ colorMode ) {
99
99
case 'rgb ' :
100
100
$ colorModeForParsing = 'rgba ' ;
101
101
$ mayHaveOptionalAlpha = true ;
@@ -107,89 +107,89 @@ private static function parseColorFunction(ParserState $oParserState): CSSFuncti
107
107
case 'rgba ' :
108
108
// This is handled identically to the following case.
109
109
case 'hsla ' :
110
- $ colorModeForParsing = $ sColorMode ;
110
+ $ colorModeForParsing = $ colorMode ;
111
111
$ mayHaveOptionalAlpha = true ;
112
112
break ;
113
113
default :
114
- $ colorModeForParsing = $ sColorMode ;
114
+ $ colorModeForParsing = $ colorMode ;
115
115
$ mayHaveOptionalAlpha = false ;
116
116
}
117
117
118
- $ bContainsVar = false ;
118
+ $ containsVar = false ;
119
119
$ isLegacySyntax = false ;
120
- $ iLength = $ oParserState ->strlen ($ colorModeForParsing );
121
- for ($ i = 0 ; $ i < $ iLength ; ++$ i ) {
122
- $ oParserState ->consumeWhiteSpace ();
123
- if ($ oParserState ->comes ('var ' )) {
124
- $ aColor [$ colorModeForParsing [$ i ]] = CSSFunction::parseIdentifierOrFunction ($ oParserState );
125
- $ bContainsVar = true ;
120
+ $ expectedArgumentCount = $ parserState ->strlen ($ colorModeForParsing );
121
+ for ($ argumentIndex = 0 ; $ argumentIndex < $ expectedArgumentCount ; ++$ argumentIndex ) {
122
+ $ parserState ->consumeWhiteSpace ();
123
+ if ($ parserState ->comes ('var ' )) {
124
+ $ colorValues [$ colorModeForParsing [$ argumentIndex ]] = CSSFunction::parseIdentifierOrFunction ($ parserState );
125
+ $ containsVar = true ;
126
126
} else {
127
- $ aColor [$ colorModeForParsing [$ i ]] = Size::parse ($ oParserState , true );
127
+ $ colorValues [$ colorModeForParsing [$ argumentIndex ]] = Size::parse ($ parserState , true );
128
128
}
129
129
130
130
// This must be done first, to consume comments as well, so that the `comes` test will work.
131
- $ oParserState ->consumeWhiteSpace ();
131
+ $ parserState ->consumeWhiteSpace ();
132
132
133
133
// With a `var` argument, the function can have fewer arguments.
134
134
// And as of CSS Color Module Level 4, the alpha argument is optional.
135
135
$ canCloseNow =
136
- $ bContainsVar ||
137
- ($ mayHaveOptionalAlpha && $ i >= $ iLength - 2 );
138
- if ($ canCloseNow && $ oParserState ->comes (') ' )) {
136
+ $ containsVar ||
137
+ ($ mayHaveOptionalAlpha && $ argumentIndex >= $ expectedArgumentCount - 2 );
138
+ if ($ canCloseNow && $ parserState ->comes (') ' )) {
139
139
break ;
140
140
}
141
141
142
142
// "Legacy" syntax is comma-delimited.
143
143
// "Modern" syntax is space-delimited, with `/` as alpha delimiter.
144
144
// They cannot be mixed.
145
- if ($ i === 0 ) {
145
+ if ($ argumentIndex === 0 ) {
146
146
// An immediate closing parenthesis is not valid.
147
- if ($ oParserState ->comes (') ' )) {
147
+ if ($ parserState ->comes (') ' )) {
148
148
throw new UnexpectedTokenException (
149
149
'Color function with no arguments ' ,
150
150
'' ,
151
151
'custom ' ,
152
- $ oParserState ->currentLine ()
152
+ $ parserState ->currentLine ()
153
153
);
154
154
}
155
- $ isLegacySyntax = $ oParserState ->comes (', ' );
155
+ $ isLegacySyntax = $ parserState ->comes (', ' );
156
156
}
157
157
158
- if ($ isLegacySyntax && $ i < ($ iLength - 1 )) {
159
- $ oParserState ->consume (', ' );
158
+ if ($ isLegacySyntax && $ argumentIndex < ($ expectedArgumentCount - 1 )) {
159
+ $ parserState ->consume (', ' );
160
160
}
161
161
162
162
// In the "modern" syntax, the alpha value must be delimited with `/`.
163
163
if (!$ isLegacySyntax ) {
164
- if ($ bContainsVar ) {
164
+ if ($ containsVar ) {
165
165
// If the `var` substitution encompasses more than one argument,
166
166
// the alpha deliminator may come at any time.
167
- if ($ oParserState ->comes ('/ ' )) {
168
- $ oParserState ->consume ('/ ' );
167
+ if ($ parserState ->comes ('/ ' )) {
168
+ $ parserState ->consume ('/ ' );
169
169
}
170
- } elseif (($ colorModeForParsing [$ i + 1 ] ?? '' ) === 'a ' ) {
170
+ } elseif (($ colorModeForParsing [$ argumentIndex + 1 ] ?? '' ) === 'a ' ) {
171
171
// Alpha value is the next expected argument.
172
172
// Since a closing parenthesis was not found, a `/` separator is now required.
173
- $ oParserState ->consume ('/ ' );
173
+ $ parserState ->consume ('/ ' );
174
174
}
175
175
}
176
176
}
177
- $ oParserState ->consume (') ' );
177
+ $ parserState ->consume (') ' );
178
178
179
179
return
180
- $ bContainsVar
181
- ? new CSSFunction ($ sColorMode , \array_values ($ aColor ), ', ' , $ oParserState ->currentLine ())
182
- : new Color ($ aColor , $ oParserState ->currentLine ());
180
+ $ containsVar
181
+ ? new CSSFunction ($ colorMode , \array_values ($ colorValues ), ', ' , $ parserState ->currentLine ())
182
+ : new Color ($ colorValues , $ parserState ->currentLine ());
183
183
}
184
184
185
- private static function mapRange (float $ fVal , float $ fFromMin , float $ fFromMax , float $ fToMin , float $ fToMax ): float
185
+ private static function mapRange (float $ value , float $ fromMin , float $ fromMax , float $ toMin , float $ toMax ): float
186
186
{
187
- $ fFromRange = $ fFromMax - $ fFromMin ;
188
- $ fToRange = $ fToMax - $ fToMin ;
189
- $ fMultiplier = $ fToRange / $ fFromRange ;
190
- $ fNewVal = $ fVal - $ fFromMin ;
191
- $ fNewVal *= $ fMultiplier ;
192
- return $ fNewVal + $ fToMin ;
187
+ $ fromRange = $ fromMax - $ fromMin ;
188
+ $ toRange = $ toMax - $ toMin ;
189
+ $ multiplier = $ toRange / $ fromRange ;
190
+ $ newValue = $ value - $ fromMin ;
191
+ $ newValue *= $ multiplier ;
192
+ return $ newValue + $ toMin ;
193
193
}
194
194
195
195
/**
@@ -201,12 +201,12 @@ public function getColor()
201
201
}
202
202
203
203
/**
204
- * @param array<int, Value|string> $aColor
204
+ * @param array<int, Value|string> $colorValues
205
205
*/
206
- public function setColor (array $ aColor ): void
206
+ public function setColor (array $ colorValues ): void
207
207
{
208
- $ this ->setName (\implode ('' , \array_keys ($ aColor )));
209
- $ this ->aComponents = $ aColor ;
208
+ $ this ->setName (\implode ('' , \array_keys ($ colorValues )));
209
+ $ this ->aComponents = $ colorValues ;
210
210
}
211
211
212
212
/**
@@ -222,19 +222,19 @@ public function __toString(): string
222
222
return $ this ->render (new OutputFormat ());
223
223
}
224
224
225
- public function render (OutputFormat $ oOutputFormat ): string
225
+ public function render (OutputFormat $ outputFormat ): string
226
226
{
227
227
// Shorthand RGB color values
228
- if ($ oOutputFormat ->getRGBHashNotation () && \implode ('' , \array_keys ($ this ->aComponents )) === 'rgb ' ) {
229
- $ sResult = \sprintf (
228
+ if ($ outputFormat ->getRGBHashNotation () && \implode ('' , \array_keys ($ this ->aComponents )) === 'rgb ' ) {
229
+ $ result = \sprintf (
230
230
'%02x%02x%02x ' ,
231
231
$ this ->aComponents ['r ' ]->getSize (),
232
232
$ this ->aComponents ['g ' ]->getSize (),
233
233
$ this ->aComponents ['b ' ]->getSize ()
234
234
);
235
- return '# ' . (($ sResult [0 ] == $ sResult [1 ]) && ($ sResult [2 ] == $ sResult [3 ]) && ($ sResult [4 ] == $ sResult [5 ])
236
- ? "$ sResult [0 ]$ sResult [2 ]$ sResult [4 ]" : $ sResult );
235
+ return '# ' . (($ result [0 ] == $ result [1 ]) && ($ result [2 ] == $ result [3 ]) && ($ result [4 ] == $ result [5 ])
236
+ ? "$ result [0 ]$ result [2 ]$ result [4 ]" : $ result );
237
237
}
238
- return parent ::render ($ oOutputFormat );
238
+ return parent ::render ($ outputFormat );
239
239
}
240
240
}
0 commit comments