forked from sass/node-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_colors.cpp
742 lines (659 loc) · 26.8 KB
/
fn_colors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#include <cctype>
#include <iomanip>
#include "ast.hpp"
#include "fn_utils.hpp"
#include "fn_colors.hpp"
namespace Sass {
namespace Functions {
bool special_number(String_Constant_Ptr s) {
if (s) {
std::string calc("calc(");
std::string var("var(");
std::string ss(s->value());
return std::equal(calc.begin(), calc.end(), ss.begin()) ||
std::equal(var.begin(), var.end(), ss.begin());
}
return false;
}
Signature rgb_sig = "rgb($red, $green, $blue)";
BUILT_IN(rgb)
{
if (
special_number(Cast<String_Constant>(env["$red"])) ||
special_number(Cast<String_Constant>(env["$green"])) ||
special_number(Cast<String_Constant>(env["$blue"]))
) {
return SASS_MEMORY_NEW(String_Constant, pstate, "rgb("
+ env["$red"]->to_string()
+ ", "
+ env["$green"]->to_string()
+ ", "
+ env["$blue"]->to_string()
+ ")"
);
}
return SASS_MEMORY_NEW(Color,
pstate,
COLOR_NUM("$red"),
COLOR_NUM("$green"),
COLOR_NUM("$blue"));
}
Signature rgba_4_sig = "rgba($red, $green, $blue, $alpha)";
BUILT_IN(rgba_4)
{
if (
special_number(Cast<String_Constant>(env["$red"])) ||
special_number(Cast<String_Constant>(env["$green"])) ||
special_number(Cast<String_Constant>(env["$blue"])) ||
special_number(Cast<String_Constant>(env["$alpha"]))
) {
return SASS_MEMORY_NEW(String_Constant, pstate, "rgba("
+ env["$red"]->to_string()
+ ", "
+ env["$green"]->to_string()
+ ", "
+ env["$blue"]->to_string()
+ ", "
+ env["$alpha"]->to_string()
+ ")"
);
}
return SASS_MEMORY_NEW(Color,
pstate,
COLOR_NUM("$red"),
COLOR_NUM("$green"),
COLOR_NUM("$blue"),
ALPHA_NUM("$alpha"));
}
Signature rgba_2_sig = "rgba($color, $alpha)";
BUILT_IN(rgba_2)
{
if (
special_number(Cast<String_Constant>(env["$color"]))
) {
return SASS_MEMORY_NEW(String_Constant, pstate, "rgba("
+ env["$color"]->to_string()
+ ", "
+ env["$alpha"]->to_string()
+ ")"
);
}
Color_Ptr c_arg = ARG("$color", Color);
if (
special_number(Cast<String_Constant>(env["$alpha"]))
) {
std::stringstream strm;
strm << "rgba("
<< (int)c_arg->r() << ", "
<< (int)c_arg->g() << ", "
<< (int)c_arg->b() << ", "
<< env["$alpha"]->to_string()
<< ")";
return SASS_MEMORY_NEW(String_Constant, pstate, strm.str());
}
Color_Ptr new_c = SASS_MEMORY_COPY(c_arg);
new_c->a(ALPHA_NUM("$alpha"));
new_c->disp("");
return new_c;
}
////////////////
// RGB FUNCTIONS
////////////////
Signature red_sig = "red($color)";
BUILT_IN(red)
{ return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->r()); }
Signature green_sig = "green($color)";
BUILT_IN(green)
{ return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->g()); }
Signature blue_sig = "blue($color)";
BUILT_IN(blue)
{ return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->b()); }
Color* colormix(Context& ctx, ParserState& pstate, Color* color1, Color* color2, double weight) {
double p = weight/100;
double w = 2*p - 1;
double a = color1->a() - color2->a();
double w1 = (((w * a == -1) ? w : (w + a)/(1 + w*a)) + 1)/2.0;
double w2 = 1 - w1;
return SASS_MEMORY_NEW(Color,
pstate,
Sass::round(w1*color1->r() + w2*color2->r(), ctx.c_options.precision),
Sass::round(w1*color1->g() + w2*color2->g(), ctx.c_options.precision),
Sass::round(w1*color1->b() + w2*color2->b(), ctx.c_options.precision),
color1->a()*p + color2->a()*(1-p));
}
Signature mix_sig = "mix($color-1, $color-2, $weight: 50%)";
BUILT_IN(mix)
{
Color_Obj color1 = ARG("$color-1", Color);
Color_Obj color2 = ARG("$color-2", Color);
double weight = DARG_U_PRCT("$weight");
return colormix(ctx, pstate, color1, color2, weight);
}
////////////////
// HSL FUNCTIONS
////////////////
// RGB to HSL helper function
struct HSL { double h; double s; double l; };
HSL rgb_to_hsl(double r, double g, double b)
{
// Algorithm from http://en.wikipedia.org/wiki/wHSL_and_HSV#Conversion_from_RGB_to_HSL_or_HSV
r /= 255.0; g /= 255.0; b /= 255.0;
double max = std::max(r, std::max(g, b));
double min = std::min(r, std::min(g, b));
double delta = max - min;
double h = 0;
double s;
double l = (max + min) / 2.0;
if (NEAR_EQUAL(max, min)) {
h = s = 0; // achromatic
}
else {
if (l < 0.5) s = delta / (max + min);
else s = delta / (2.0 - max - min);
if (r == max) h = (g - b) / delta + (g < b ? 6 : 0);
else if (g == max) h = (b - r) / delta + 2;
else if (b == max) h = (r - g) / delta + 4;
}
HSL hsl_struct;
hsl_struct.h = h / 6 * 360;
hsl_struct.s = s * 100;
hsl_struct.l = l * 100;
return hsl_struct;
}
// hue to RGB helper function
double h_to_rgb(double m1, double m2, double h) {
while (h < 0) h += 1;
while (h > 1) h -= 1;
if (h*6.0 < 1) return m1 + (m2 - m1)*h*6;
if (h*2.0 < 1) return m2;
if (h*3.0 < 2) return m1 + (m2 - m1) * (2.0/3.0 - h)*6;
return m1;
}
Color_Ptr hsla_impl(double h, double s, double l, double a, Context& ctx, ParserState pstate)
{
h /= 360.0;
s /= 100.0;
l /= 100.0;
if (l < 0) l = 0;
if (s < 0) s = 0;
if (l > 1) l = 1;
if (s > 1) s = 1;
while (h < 0) h += 1;
while (h > 1) h -= 1;
// if saturation is exacly zero, we loose
// information for hue, since it will evaluate
// to zero if converted back from rgb. Setting
// saturation to a very tiny number solves this.
if (s == 0) s = 1e-10;
// Algorithm from the CSS3 spec: http://www.w3.org/TR/css3-color/#hsl-color.
double m2;
if (l <= 0.5) m2 = l*(s+1.0);
else m2 = (l+s)-(l*s);
double m1 = (l*2.0)-m2;
// round the results -- consider moving this into the Color constructor
double r = (h_to_rgb(m1, m2, h + 1.0/3.0) * 255.0);
double g = (h_to_rgb(m1, m2, h) * 255.0);
double b = (h_to_rgb(m1, m2, h - 1.0/3.0) * 255.0);
return SASS_MEMORY_NEW(Color, pstate, r, g, b, a);
}
Signature hsl_sig = "hsl($hue, $saturation, $lightness)";
BUILT_IN(hsl)
{
if (
special_number(Cast<String_Constant>(env["$hue"])) ||
special_number(Cast<String_Constant>(env["$saturation"])) ||
special_number(Cast<String_Constant>(env["$lightness"]))
) {
return SASS_MEMORY_NEW(String_Constant, pstate, "hsl("
+ env["$hue"]->to_string()
+ ", "
+ env["$saturation"]->to_string()
+ ", "
+ env["$lightness"]->to_string()
+ ")"
);
}
return hsla_impl(ARGVAL("$hue"),
ARGVAL("$saturation"),
ARGVAL("$lightness"),
1.0,
ctx,
pstate);
}
Signature hsla_sig = "hsla($hue, $saturation, $lightness, $alpha)";
BUILT_IN(hsla)
{
if (
special_number(Cast<String_Constant>(env["$hue"])) ||
special_number(Cast<String_Constant>(env["$saturation"])) ||
special_number(Cast<String_Constant>(env["$lightness"])) ||
special_number(Cast<String_Constant>(env["$alpha"]))
) {
return SASS_MEMORY_NEW(String_Constant, pstate, "hsla("
+ env["$hue"]->to_string()
+ ", "
+ env["$saturation"]->to_string()
+ ", "
+ env["$lightness"]->to_string()
+ ", "
+ env["$alpha"]->to_string()
+ ")"
);
}
return hsla_impl(ARGVAL("$hue"),
ARGVAL("$saturation"),
ARGVAL("$lightness"),
ARGVAL("$alpha"),
ctx,
pstate);
}
Signature hue_sig = "hue($color)";
BUILT_IN(hue)
{
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return SASS_MEMORY_NEW(Number, pstate, hsl_color.h, "deg");
}
Signature saturation_sig = "saturation($color)";
BUILT_IN(saturation)
{
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return SASS_MEMORY_NEW(Number, pstate, hsl_color.s, "%");
}
Signature lightness_sig = "lightness($color)";
BUILT_IN(lightness)
{
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return SASS_MEMORY_NEW(Number, pstate, hsl_color.l, "%");
}
Signature adjust_hue_sig = "adjust-hue($color, $degrees)";
BUILT_IN(adjust_hue)
{
Color_Ptr rgb_color = ARG("$color", Color);
double degrees = ARGVAL("$degrees");
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return hsla_impl(hsl_color.h + degrees,
hsl_color.s,
hsl_color.l,
rgb_color->a(),
ctx,
pstate);
}
Signature lighten_sig = "lighten($color, $amount)";
BUILT_IN(lighten)
{
Color_Ptr rgb_color = ARG("$color", Color);
double amount = DARG_U_PRCT("$amount");
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
//Check lightness is not negative before lighten it
double hslcolorL = hsl_color.l;
if (hslcolorL < 0) {
hslcolorL = 0;
}
return hsla_impl(hsl_color.h,
hsl_color.s,
hslcolorL + amount,
rgb_color->a(),
ctx,
pstate);
}
Signature darken_sig = "darken($color, $amount)";
BUILT_IN(darken)
{
Color_Ptr rgb_color = ARG("$color", Color);
double amount = DARG_U_PRCT("$amount");
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
//Check lightness if not over 100, before darken it
double hslcolorL = hsl_color.l;
if (hslcolorL > 100) {
hslcolorL = 100;
}
return hsla_impl(hsl_color.h,
hsl_color.s,
hslcolorL - amount,
rgb_color->a(),
ctx,
pstate);
}
Signature saturate_sig = "saturate($color, $amount: false)";
BUILT_IN(saturate)
{
// CSS3 filter function overload: pass literal through directly
if (!Cast<Number>(env["$amount"])) {
return SASS_MEMORY_NEW(String_Quoted, pstate, "saturate(" + env["$color"]->to_string(ctx.c_options) + ")");
}
double amount = DARG_U_PRCT("$amount");
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
double hslcolorS = hsl_color.s + amount;
// Saturation cannot be below 0 or above 100
if (hslcolorS < 0) {
hslcolorS = 0;
}
if (hslcolorS > 100) {
hslcolorS = 100;
}
return hsla_impl(hsl_color.h,
hslcolorS,
hsl_color.l,
rgb_color->a(),
ctx,
pstate);
}
Signature desaturate_sig = "desaturate($color, $amount)";
BUILT_IN(desaturate)
{
Color_Ptr rgb_color = ARG("$color", Color);
double amount = DARG_U_PRCT("$amount");
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
double hslcolorS = hsl_color.s - amount;
// Saturation cannot be below 0 or above 100
if (hslcolorS <= 0) {
hslcolorS = 0;
}
if (hslcolorS > 100) {
hslcolorS = 100;
}
return hsla_impl(hsl_color.h,
hslcolorS,
hsl_color.l,
rgb_color->a(),
ctx,
pstate);
}
Signature grayscale_sig = "grayscale($color)";
BUILT_IN(grayscale)
{
// CSS3 filter function overload: pass literal through directly
Number_Ptr amount = Cast<Number>(env["$color"]);
if (amount) {
return SASS_MEMORY_NEW(String_Quoted, pstate, "grayscale(" + amount->to_string(ctx.c_options) + ")");
}
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return hsla_impl(hsl_color.h,
0.0,
hsl_color.l,
rgb_color->a(),
ctx,
pstate);
}
Signature complement_sig = "complement($color)";
BUILT_IN(complement)
{
Color_Ptr rgb_color = ARG("$color", Color);
HSL hsl_color = rgb_to_hsl(rgb_color->r(),
rgb_color->g(),
rgb_color->b());
return hsla_impl(hsl_color.h - 180.0,
hsl_color.s,
hsl_color.l,
rgb_color->a(),
ctx,
pstate);
}
Signature invert_sig = "invert($color, $weight: 100%)";
BUILT_IN(invert)
{
// CSS3 filter function overload: pass literal through directly
Number_Ptr amount = Cast<Number>(env["$color"]);
if (amount) {
return SASS_MEMORY_NEW(String_Quoted, pstate, "invert(" + amount->to_string(ctx.c_options) + ")");
}
double weight = DARG_U_PRCT("$weight");
Color_Ptr rgb_color = ARG("$color", Color);
Color_Obj inv = SASS_MEMORY_NEW(Color,
pstate,
255 - rgb_color->r(),
255 - rgb_color->g(),
255 - rgb_color->b(),
rgb_color->a());
return colormix(ctx, pstate, inv, rgb_color, weight);
}
////////////////////
// OPACITY FUNCTIONS
////////////////////
Signature alpha_sig = "alpha($color)";
Signature opacity_sig = "opacity($color)";
BUILT_IN(alpha)
{
String_Constant_Ptr ie_kwd = Cast<String_Constant>(env["$color"]);
if (ie_kwd) {
return SASS_MEMORY_NEW(String_Quoted, pstate, "alpha(" + ie_kwd->value() + ")");
}
// CSS3 filter function overload: pass literal through directly
Number_Ptr amount = Cast<Number>(env["$color"]);
if (amount) {
return SASS_MEMORY_NEW(String_Quoted, pstate, "opacity(" + amount->to_string(ctx.c_options) + ")");
}
return SASS_MEMORY_NEW(Number, pstate, ARG("$color", Color)->a());
}
Signature opacify_sig = "opacify($color, $amount)";
Signature fade_in_sig = "fade-in($color, $amount)";
BUILT_IN(opacify)
{
Color_Ptr color = ARG("$color", Color);
double amount = DARG_U_FACT("$amount");
double alpha = std::min(color->a() + amount, 1.0);
return SASS_MEMORY_NEW(Color,
pstate,
color->r(),
color->g(),
color->b(),
alpha);
}
Signature transparentize_sig = "transparentize($color, $amount)";
Signature fade_out_sig = "fade-out($color, $amount)";
BUILT_IN(transparentize)
{
Color_Ptr color = ARG("$color", Color);
double amount = DARG_U_FACT("$amount");
double alpha = std::max(color->a() - amount, 0.0);
return SASS_MEMORY_NEW(Color,
pstate,
color->r(),
color->g(),
color->b(),
alpha);
}
////////////////////////
// OTHER COLOR FUNCTIONS
////////////////////////
Signature adjust_color_sig = "adjust-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
BUILT_IN(adjust_color)
{
Color_Ptr color = ARG("$color", Color);
Number_Ptr r = Cast<Number>(env["$red"]);
Number_Ptr g = Cast<Number>(env["$green"]);
Number_Ptr b = Cast<Number>(env["$blue"]);
Number_Ptr h = Cast<Number>(env["$hue"]);
Number_Ptr s = Cast<Number>(env["$saturation"]);
Number_Ptr l = Cast<Number>(env["$lightness"]);
Number_Ptr a = Cast<Number>(env["$alpha"]);
bool rgb = r || g || b;
bool hsl = h || s || l;
if (rgb && hsl) {
error("Cannot specify HSL and RGB values for a color at the same time for `adjust-color'", pstate, traces);
}
if (rgb) {
double rr = r ? DARG_R_BYTE("$red") : 0;
double gg = g ? DARG_R_BYTE("$green") : 0;
double bb = b ? DARG_R_BYTE("$blue") : 0;
double aa = a ? DARG_R_FACT("$alpha") : 0;
return SASS_MEMORY_NEW(Color,
pstate,
color->r() + rr,
color->g() + gg,
color->b() + bb,
color->a() + aa);
}
if (hsl) {
HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
double ss = s ? DARG_R_PRCT("$saturation") : 0;
double ll = l ? DARG_R_PRCT("$lightness") : 0;
double aa = a ? DARG_R_FACT("$alpha") : 0;
return hsla_impl(hsl_struct.h + (h ? h->value() : 0),
hsl_struct.s + ss,
hsl_struct.l + ll,
color->a() + aa,
ctx,
pstate);
}
if (a) {
return SASS_MEMORY_NEW(Color,
pstate,
color->r(),
color->g(),
color->b(),
color->a() + (a ? a->value() : 0));
}
error("not enough arguments for `adjust-color'", pstate, traces);
// unreachable
return color;
}
Signature scale_color_sig = "scale-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
BUILT_IN(scale_color)
{
Color_Ptr color = ARG("$color", Color);
Number_Ptr r = Cast<Number>(env["$red"]);
Number_Ptr g = Cast<Number>(env["$green"]);
Number_Ptr b = Cast<Number>(env["$blue"]);
Number_Ptr h = Cast<Number>(env["$hue"]);
Number_Ptr s = Cast<Number>(env["$saturation"]);
Number_Ptr l = Cast<Number>(env["$lightness"]);
Number_Ptr a = Cast<Number>(env["$alpha"]);
bool rgb = r || g || b;
bool hsl = h || s || l;
if (rgb && hsl) {
error("Cannot specify HSL and RGB values for a color at the same time for `scale-color'", pstate, traces);
}
if (rgb) {
double rscale = (r ? DARG_R_PRCT("$red") : 0.0) / 100.0;
double gscale = (g ? DARG_R_PRCT("$green") : 0.0) / 100.0;
double bscale = (b ? DARG_R_PRCT("$blue") : 0.0) / 100.0;
double ascale = (a ? DARG_R_PRCT("$alpha") : 0.0) / 100.0;
return SASS_MEMORY_NEW(Color,
pstate,
color->r() + rscale * (rscale > 0.0 ? 255 - color->r() : color->r()),
color->g() + gscale * (gscale > 0.0 ? 255 - color->g() : color->g()),
color->b() + bscale * (bscale > 0.0 ? 255 - color->b() : color->b()),
color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a()));
}
if (hsl) {
double hscale = (h ? DARG_R_PRCT("$hue") : 0.0) / 100.0;
double sscale = (s ? DARG_R_PRCT("$saturation") : 0.0) / 100.0;
double lscale = (l ? DARG_R_PRCT("$lightness") : 0.0) / 100.0;
double ascale = (a ? DARG_R_PRCT("$alpha") : 0.0) / 100.0;
HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
hsl_struct.h += hscale * (hscale > 0.0 ? 360.0 - hsl_struct.h : hsl_struct.h);
hsl_struct.s += sscale * (sscale > 0.0 ? 100.0 - hsl_struct.s : hsl_struct.s);
hsl_struct.l += lscale * (lscale > 0.0 ? 100.0 - hsl_struct.l : hsl_struct.l);
double alpha = color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a());
return hsla_impl(hsl_struct.h, hsl_struct.s, hsl_struct.l, alpha, ctx, pstate);
}
if (a) {
double ascale = (DARG_R_PRCT("$alpha")) / 100.0;
return SASS_MEMORY_NEW(Color,
pstate,
color->r(),
color->g(),
color->b(),
color->a() + ascale * (ascale > 0.0 ? 1.0 - color->a() : color->a()));
}
error("not enough arguments for `scale-color'", pstate, traces);
// unreachable
return color;
}
Signature change_color_sig = "change-color($color, $red: false, $green: false, $blue: false, $hue: false, $saturation: false, $lightness: false, $alpha: false)";
BUILT_IN(change_color)
{
Color_Ptr color = ARG("$color", Color);
Number_Ptr r = Cast<Number>(env["$red"]);
Number_Ptr g = Cast<Number>(env["$green"]);
Number_Ptr b = Cast<Number>(env["$blue"]);
Number_Ptr h = Cast<Number>(env["$hue"]);
Number_Ptr s = Cast<Number>(env["$saturation"]);
Number_Ptr l = Cast<Number>(env["$lightness"]);
Number_Ptr a = Cast<Number>(env["$alpha"]);
bool rgb = r || g || b;
bool hsl = h || s || l;
if (rgb && hsl) {
error("Cannot specify HSL and RGB values for a color at the same time for `change-color'", pstate, traces);
}
if (rgb) {
return SASS_MEMORY_NEW(Color,
pstate,
r ? DARG_U_BYTE("$red") : color->r(),
g ? DARG_U_BYTE("$green") : color->g(),
b ? DARG_U_BYTE("$blue") : color->b(),
a ? DARG_U_BYTE("$alpha") : color->a());
}
if (hsl) {
HSL hsl_struct = rgb_to_hsl(color->r(), color->g(), color->b());
if (h) hsl_struct.h = std::fmod(h->value(), 360.0);
if (s) hsl_struct.s = DARG_U_PRCT("$saturation");
if (l) hsl_struct.l = DARG_U_PRCT("$lightness");
double alpha = a ? DARG_U_FACT("$alpha") : color->a();
return hsla_impl(hsl_struct.h, hsl_struct.s, hsl_struct.l, alpha, ctx, pstate);
}
if (a) {
double alpha = DARG_U_FACT("$alpha");
return SASS_MEMORY_NEW(Color,
pstate,
color->r(),
color->g(),
color->b(),
alpha);
}
error("not enough arguments for `change-color'", pstate, traces);
// unreachable
return color;
}
template <size_t range>
static double cap_channel(double c) {
if (c > range) return range;
else if (c < 0) return 0;
else return c;
}
Signature ie_hex_str_sig = "ie-hex-str($color)";
BUILT_IN(ie_hex_str)
{
Color_Ptr c = ARG("$color", Color);
double r = cap_channel<0xff>(c->r());
double g = cap_channel<0xff>(c->g());
double b = cap_channel<0xff>(c->b());
double a = cap_channel<1> (c->a()) * 255;
std::stringstream ss;
ss << '#' << std::setw(2) << std::setfill('0');
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(a, ctx.c_options.precision));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(r, ctx.c_options.precision));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(g, ctx.c_options.precision));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(b, ctx.c_options.precision));
std::string result(ss.str());
for (size_t i = 0, L = result.length(); i < L; ++i) {
result[i] = std::toupper(result[i]);
}
return SASS_MEMORY_NEW(String_Quoted, pstate, result);
}
}
}