• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dragomano / scss-php / 23594295754

26 Mar 2026 12:19PM UTC coverage: 84.536%. First build
23594295754

push

github

web-flow
Merge pull request #51 from dragomano/refactor

Rewrite compiler

10140 of 12016 new or added lines in 162 files covered. (84.39%)

10272 of 12151 relevant lines covered (84.54%)

83.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.78
/src/Builtins/SassColorModule.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Bugo\SCSS\Builtins;
6

7
use Bugo\SCSS\Builtins\Color\Ast\ColorAstParser;
8
use Bugo\SCSS\Builtins\Color\Ast\ColorAstReader;
9
use Bugo\SCSS\Builtins\Color\Ast\ColorAstWriter;
10
use Bugo\SCSS\Builtins\Color\ColorBundleAdapter;
11
use Bugo\SCSS\Builtins\Color\Conversion\ColorNodeConverter;
12
use Bugo\SCSS\Builtins\Color\Conversion\ColorSpaceInterop;
13
use Bugo\SCSS\Builtins\Color\Conversion\CssColorFunctionConverter;
14
use Bugo\SCSS\Builtins\Color\Conversion\HexColorConverter;
15
use Bugo\SCSS\Builtins\Color\Operations\ColorChannelReader;
16
use Bugo\SCSS\Builtins\Color\Operations\ColorConstructorFunctions;
17
use Bugo\SCSS\Builtins\Color\Operations\ColorFunctionEvaluator;
18
use Bugo\SCSS\Builtins\Color\Support\ColorArgumentParser;
19
use Bugo\SCSS\Builtins\Color\Support\ColorChannelSchema;
20
use Bugo\SCSS\Builtins\Color\Support\ColorValueFormatter;
21
use Bugo\SCSS\Contracts\Color\ColorBundleInterface;
22
use Bugo\SCSS\Exceptions\UnknownSassFunctionException;
23
use Bugo\SCSS\Nodes\AstNode;
24
use Bugo\SCSS\Runtime\BuiltinCallContext;
25

26
final class SassColorModule extends AbstractModule
27
{
28
    private const FUNCTIONS = [
29
        'adjust',
30
        'alpha',
31
        'blackness',
32
        'blue',
33
        'change',
34
        'channel',
35
        'complement',
36
        'grayscale',
37
        'green',
38
        'hue',
39
        'hwb',
40
        'ie-hex-str',
41
        'invert',
42
        'is-in-gamut',
43
        'is-legacy',
44
        'is-missing',
45
        'is-powerless',
46
        'lightness',
47
        'mix',
48
        'opacity',
49
        'red',
50
        'same',
51
        'saturation',
52
        'scale',
53
        'space',
54
        'to-gamut',
55
        'to-space',
56
        'whiteness',
57
    ];
58

59
    private const GLOBAL_FUNCTIONS = [
60
        'adjust-color',
61
        'adjust-hue',
62
        'alpha',
63
        'blackness',
64
        'blue',
65
        'change-color',
66
        'color',
67
        'complement',
68
        'darken',
69
        'desaturate',
70
        'fade-in',
71
        'fade-out',
72
        'grayscale',
73
        'green',
74
        'hsl',
75
        'hsla',
76
        'hue',
77
        'hwb',
78
        'ie-hex-str',
79
        'invert',
80
        'lab',
81
        'lch',
82
        'lighten',
83
        'lightness',
84
        'mix',
85
        'oklab',
86
        'oklch',
87
        'opacify',
88
        'opacity',
89
        'red',
90
        'rgb',
91
        'rgba',
92
        'saturate',
93
        'saturation',
94
        'scale-color',
95
        'transparentize',
96
        'whiteness',
97
    ];
98

99
    private const GLOBAL_ALIASES = [
100
        'color-channel' => 'channel',
101
        'rgba'          => 'legacy-rgba',
102
    ];
103

104
    private readonly ColorArgumentParser $argumentParser;
105

106
    private readonly ColorNodeConverter $converter;
107

108
    private readonly ColorSpaceInterop $spaceInterop;
109

110
    private readonly ColorChannelReader $channelReader;
111

112
    private readonly ColorFunctionEvaluator $functions;
113

114
    private readonly ColorConstructorFunctions $constructors;
115

116
    private readonly ColorValueFormatter $formatter;
117

118
    private readonly ColorAstWriter $astWriter;
119

120
    private readonly ColorAstReader $astReader;
121

122
    public function __construct(
123
        private readonly HexColorConverter $hexColorConverter = new HexColorConverter(),
124
        ColorBundleInterface $bundle = new ColorBundleAdapter()
125
    ) {
126
        $errorCtx = fn(string $f): string => $this->builtinErrorContext($f);
355 ✔
127
        $isGlobal = fn(): bool => $this->isGlobalBuiltinCall();
355 ✔
128
        $warn     = function (?BuiltinCallContext $ctx, string $s, bool $multipleSuggestions = false): void {
355 ✔
129
            $this->warnAboutDeprecatedBuiltinFunction($ctx, $s, 'color', $multipleSuggestions);
30 ✔
130
        };
355 ✔
131

132
        $converter    = $bundle->getConverter();
355 ✔
133
        $literal      = $bundle->getLiteral();
355 ✔
134
        $manipulator  = $bundle->getManipulator();
355 ✔
135

136
        $this->argumentParser = new ColorArgumentParser($converter, $errorCtx);
355 ✔
137
        $this->formatter      = new ColorValueFormatter($converter);
355 ✔
138
        $this->astWriter      = new ColorAstWriter($converter, $literal);
355 ✔
139

140
        $cssColorFunctionConverter = new CssColorFunctionConverter($converter);
355 ✔
141

142
        $this->converter = new ColorNodeConverter(
355 ✔
143
            $this->hexColorConverter,
355 ✔
144
            $cssColorFunctionConverter,
355 ✔
145
            $converter,
355 ✔
146
            $literal,
355 ✔
147
            new ColorAstParser(),
355 ✔
148
            $errorCtx
355 ✔
149
        );
355 ✔
150

151
        $this->astReader = new ColorAstReader(
355 ✔
152
            $this->argumentParser,
355 ✔
153
            $this->converter,
355 ✔
154
            $this->hexColorConverter,
355 ✔
155
            $converter,
355 ✔
156
        );
355 ✔
157

158
        $this->spaceInterop = new ColorSpaceInterop(
355 ✔
159
            $this->argumentParser,
355 ✔
160
            $this->converter,
355 ✔
161
            $this->astWriter,
355 ✔
162
            $this->astReader,
355 ✔
163
            $this->hexColorConverter,
355 ✔
164
            $converter,
355 ✔
165
            new ColorChannelSchema(),
355 ✔
166
            $errorCtx
355 ✔
167
        );
355 ✔
168

169
        $this->channelReader = new ColorChannelReader(
355 ✔
170
            $this->argumentParser,
355 ✔
171
            $this->converter,
355 ✔
172
            $this->formatter,
355 ✔
173
            $converter,
355 ✔
174
            new ColorChannelSchema(),
355 ✔
175
            $errorCtx,
355 ✔
176
            $isGlobal,
355 ✔
177
            $warn
355 ✔
178
        );
355 ✔
179

180
        $this->functions = new ColorFunctionEvaluator(
355 ✔
181
            $this->argumentParser,
355 ✔
182
            $this->converter,
355 ✔
183
            $this->astWriter,
355 ✔
184
            $this->astReader,
355 ✔
185
            $manipulator,
355 ✔
186
            $this->spaceInterop,
355 ✔
187
            $this->formatter,
355 ✔
188
            $converter,
355 ✔
189
            $warn
355 ✔
190
        );
355 ✔
191

192
        $this->constructors = new ColorConstructorFunctions(
355 ✔
193
            $this->argumentParser,
355 ✔
194
            $this->converter,
355 ✔
195
            $this->astWriter,
355 ✔
196
            $errorCtx
355 ✔
197
        );
355 ✔
198
    }
199

200
    public function getName(): string
201
    {
202
        return 'color';
286 ✔
203
    }
204

205
    public function getFunctions(): array
206
    {
207
        return self::FUNCTIONS;
61 ✔
208
    }
209

210
    public function getGlobalAliases(): array
211
    {
212
        return $this->globalAliases(self::GLOBAL_FUNCTIONS, self::GLOBAL_ALIASES);
285 ✔
213
    }
214

215
    /**
216
     * @param array<int, AstNode> $positional
217
     * @param array<string, AstNode> $named
218
     */
219
    public function call(string $name, array $positional, array $named, ?BuiltinCallContext $context = null): AstNode
220
    {
221
        $previousDisplayName = $this->beginBuiltinCall($name, $context);
207 ✔
222

223
        try {
224
            return match ($name) {
207 ✔
225
                'adjust-hue'             => $this->functions->adjustHue($positional, $context),
3 ✔
226
                'adjust', 'adjust-color' => $this->functions->adjustColor($positional, $named),
6 ✔
227
                'alpha', 'opacity'       => $this->channelReader->channelAlpha($positional, $name, $context),
5 ✔
228
                'blackness',
207 ✔
229
                'blue',
207 ✔
230
                'green',
207 ✔
231
                'hue',
207 ✔
232
                'lightness',
207 ✔
233
                'red',
207 ✔
234
                'saturation',
207 ✔
235
                'whiteness'              => $this->deprecatedChannelFunction($name, $positional, $context),
11 ✔
236
                'change', 'change-color' => $this->functions->changeColor($positional, $named),
6 ✔
237
                'channel'                => $this->channelReader->channel($positional, $named),
3 ✔
238
                'color'                  => $this->constructors->colorFunction($positional),
20 ✔
239
                'complement'             => $this->functions->complement($positional),
5 ✔
240
                'darken',
207 ✔
241
                'desaturate',
207 ✔
242
                'lighten',
207 ✔
243
                'saturate'               => $this->legacyChannelAdjustment($name, $positional, $context),
15 ✔
244
                'grayscale'              => $this->functions->grayscale($positional),
6 ✔
245
                'hsl'                    => $this->constructors->hslFunction($positional),
12 ✔
246
                'hsla'                   => $this->constructors->hslaFunction($positional),
4 ✔
247
                'hwb'                    => $this->constructors->hwbFunction($positional),
6 ✔
248
                'ie-hex-str'             => $this->constructors->ieHexStr($positional),
5 ✔
249
                'invert'                 => $this->functions->invert($positional, $named),
4 ✔
250
                'is-in-gamut'            => $this->channelReader->isInGamut($positional),
6 ✔
251
                'is-legacy'              => $this->channelReader->isLegacy($positional),
2 ✔
252
                'is-missing'             => $this->channelReader->isMissing($positional),
4 ✔
253
                'is-powerless'           => $this->channelReader->isPowerless($positional, $named),
2 ✔
254
                'lab'                    => $this->constructors->labFunction($positional),
7 ✔
255
                'lch'                    => $this->constructors->lchFunction($positional),
11 ✔
256
                'legacy-rgba'            => $this->constructors->legacyRgbaFunction($positional),
25 ✔
257
                'mix'                    => $this->functions->mix($positional, $named),
20 ✔
258
                'oklab'                  => $this->constructors->oklabFunction($positional),
6 ✔
259
                'oklch'                  => $this->constructors->oklchFunction($positional),
18 ✔
260
                'opacify',
207 ✔
261
                'fade-in',
207 ✔
262
                'transparentize',
207 ✔
263
                'fade-out'               => $this->legacyAlphaAdjustment($name, $positional, $context),
7 ✔
264
                'rgb'                    => $this->constructors->rgbFunction($positional),
25 ✔
265
                'rgba'                   => $this->constructors->rgbaFunction($positional),
2 ✔
266
                'same'                   => $this->functions->same($positional),
4 ✔
267
                'scale', 'scale-color'   => $this->functions->scaleColor($positional, $named),
11 ✔
268
                'space'                  => $this->channelReader->space($positional),
2 ✔
269
                'to-gamut'               => $this->spaceInterop->toGamut($positional, $named),
6 ✔
270
                'to-space'               => $this->spaceInterop->toSpace($positional),
23 ✔
271
                default                  => throw new UnknownSassFunctionException('color', $name),
192 ✔
272
            };
207 ✔
273
        } finally {
274
            $this->endBuiltinCall($previousDisplayName);
207 ✔
275
        }
276
    }
277

278
    /**
279
     * @param array<int, AstNode> $positional
280
     */
281
    private function deprecatedChannelFunction(string $name, array $positional, ?BuiltinCallContext $context): AstNode
282
    {
283
        return match ($name) {
11 ✔
284
            'red', 'green', 'blue' => $this->channelReader->deprecatedChannelValue(
4 ✔
285
                $positional,
4 ✔
286
                'rgb',
4 ✔
287
                $name,
4 ✔
288
                $name,
4 ✔
289
                $context
4 ✔
290
            ),
4 ✔
291
            'hue', 'saturation', 'lightness' => $this->channelReader->deprecatedChannelValue(
4 ✔
292
                $positional,
4 ✔
293
                'hsl',
4 ✔
294
                $name,
4 ✔
295
                $name,
4 ✔
296
                $context
4 ✔
297
            ),
4 ✔
298
            'whiteness', 'blackness' => $this->channelReader->deprecatedChannelValue(
5 ✔
299
                $positional,
5 ✔
300
                'hwb',
5 ✔
301
                $name,
5 ✔
302
                $name,
5 ✔
303
                $context
5 ✔
304
            ),
5 ✔
305
            default => throw new UnknownSassFunctionException('color', $name),
11 ✔
306
        };
11 ✔
307
    }
308

309
    /**
310
     * @param array<int, AstNode> $positional
311
     */
312
    private function legacyAlphaAdjustment(string $name, array $positional, ?BuiltinCallContext $context): AstNode
313
    {
314
        $direction = match ($name) {
7 ✔
315
            'opacify',
7 ✔
316
            'fade-in'  => 1,
4 ✔
317
            'transparentize',
7 ✔
318
            'fade-out' => -1,
4 ✔
NEW
319
            default    => throw new UnknownSassFunctionException('color', $name),
×
320
        };
7 ✔
321

322
        return $this->functions->adjustAlphaChannel($positional, $direction, $name, $context);
7 ✔
323
    }
324

325
    /**
326
     * @param array<int, AstNode> $positional
327
     */
328
    private function legacyChannelAdjustment(string $name, array $positional, ?BuiltinCallContext $context): AstNode
329
    {
330
        [$channel, $direction, $allowCssDefer] = match ($name) {
15 ✔
331
            'lighten'    => ['lightness', 1, false],
5 ✔
332
            'darken'     => ['lightness', -1, false],
5 ✔
333
            'saturate'   => ['saturation', 1, true],
7 ✔
334
            'desaturate' => ['saturation', -1, true],
2 ✔
NEW
335
            default      => throw new UnknownSassFunctionException('color', $name),
×
336
        };
15 ✔
337

338
        return $this->functions->adjustColorChannelByPercent(
15 ✔
339
            $positional,
15 ✔
340
            $channel,
15 ✔
341
            $direction,
15 ✔
342
            $name,
15 ✔
343
            $allowCssDefer,
15 ✔
344
            $context
15 ✔
345
        );
15 ✔
346
    }
347
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc