Skip to content

Commit e559727

Browse files
committed
Round stringified color values
- Round by 4 decimals for all percentages and alpha values - Round by 0 decimals for all legacy rgb values
1 parent 604e99a commit e559727

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

lib/color.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ export default class Color {
127127
const color = color2hsl(this.color);
128128
const isOpaque = color.alpha === 1;
129129
const hue = color.hue;
130-
const saturation = color.saturation * 100;
131-
const lightness = color.lightness * 100;
132-
const alpha = color.alpha * 100;
130+
const saturation = round(color.saturation * 100, 4);
131+
const lightness = round(color.lightness * 100, 4);
132+
const alpha = round(color.alpha * 100, 4);
133133

134134
return `hsl(${hue} ${saturation}% ${lightness}%${isOpaque
135135
? ''
@@ -140,9 +140,9 @@ export default class Color {
140140
const color = color2hwb(this.color);
141141
const isOpaque = color.alpha === 1;
142142
const hue = color.hue;
143-
const whiteness = color.whiteness * 100;
144-
const blackness = color.blackness * 100;
145-
const alpha = color.alpha * 100;
143+
const whiteness = round(color.whiteness * 100, 4);
144+
const blackness = round(color.blackness * 100, 4);
145+
const alpha = round(color.alpha * 100, 4);
146146

147147
return `hwb(${hue} ${whiteness}% ${blackness}%${isOpaque
148148
? ''
@@ -152,10 +152,10 @@ export default class Color {
152152
toRGB() {
153153
const color = color2rgb(this.color);
154154
const isOpaque = color.alpha === 1;
155-
const red = color.red * 100;
156-
const green = color.green * 100;
157-
const blue = color.blue * 100;
158-
const alpha = color.alpha * 100;
155+
const red = round(color.red * 100, 4);
156+
const green = round(color.green * 100, 4);
157+
const blue = round(color.blue * 100, 4);
158+
const alpha = round(color.alpha * 100, 4);
159159

160160
return `rgb(${red}% ${green}% ${blue}%${isOpaque
161161
? ''
@@ -166,10 +166,10 @@ export default class Color {
166166
const color = color2rgb(this.color);
167167
const isOpaque = color.alpha === 1;
168168
const name = isOpaque ? 'rgb' : 'rgba';
169-
const red = color.red * 255;
170-
const green = color.green * 255;
171-
const blue = color.blue * 255;
172-
const alpha = color.alpha;
169+
const red = round(color.red * 255, 0);
170+
const green = round(color.green * 255, 0);
171+
const blue = round(color.blue * 255, 0);
172+
const alpha = round(color.alpha, 4);
173173

174174
return `${name}(${red}, ${green}, ${blue}${isOpaque
175175
? ''
@@ -547,6 +547,13 @@ function lvw2saturation(lightness, value, whiteness) {
547547
: (value - whiteness) / (2 - value - whiteness);
548548
}
549549

550+
/* Round to decimal place
551+
/* ========================================================================== */
552+
553+
function round(value, decimals) {
554+
return Number(`${Math.round(`${value}e${decimals}`)}e-${decimals}`);
555+
}
556+
550557
/* Match
551558
/* ========================================================================== */
552559

test/basic.colors.expect.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ test-color-mod {
2323
}
2424

2525
test-red-green-blue-alpha-adjuster {
26-
color: rgb(100% 0% 7.8431372549019605%);
26+
color: rgb(100% 0% 7.8431%);
2727
color: rgb(100% 0% 20%);
28-
color: rgb(100% 7.8431372549019605% 0%);
28+
color: rgb(100% 7.8431% 0%);
2929
color: rgb(100% 20% 0%);
30-
color: rgb(92.15686274509804% 0% 0%);
30+
color: rgb(92.1569% 0% 0%);
3131
color: rgb(80% 0% 0%);
3232
color: rgb(100% 0% 0% / 20%);
3333
}
@@ -92,5 +92,5 @@ test-contrast-adjuster {
9292
}
9393

9494
test-combination-adjuster {
95-
color: rgb(34.56764855961519% 17.846405228758172% 44.56764855961519%);
95+
color: rgb(34.5676% 17.8464% 44.5676%);
9696
}

test/basic.expect.css

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test-red-green-blue-alpha-adjuster {
2727
color: rgb(255, 0, 51);
2828
color: rgb(255, 20, 0);
2929
color: rgb(255, 51, 0);
30-
color: rgb(235.00000000000003, 0, 0);
30+
color: rgb(235, 0, 0);
3131
color: rgb(204, 0, 0);
3232
color: rgba(255, 0, 0, 0.2);
3333
}
@@ -37,28 +37,28 @@ test-rgb-adjuster {
3737
color: rgb(255, 255, 0);
3838
color: rgb(0, 0, 0);
3939
color: rgb(0, 0, 0);
40-
color: rgb(127.5, 0, 0);
40+
color: rgb(128, 0, 0);
4141
}
4242

4343
test-hue-adjuster {
4444
color: rgb(255, 85, 255);
4545
color: rgb(255, 85, 255);
4646
color: rgb(255, 85, 255);
4747
color: rgb(255, 85, 255);
48-
color: rgb(0, 0, 84.99999999999993);
49-
color: rgb(0, 0, 84.99999999999993);
48+
color: rgb(0, 0, 85);
49+
color: rgb(0, 0, 85);
5050
color: rgb(255, 0, 255);
5151
color: rgb(255, 0, 255);
5252
}
5353

5454
test-lightness-saturation-adjuster {
5555
color: rgb(102, 0, 102);
56-
color: rgb(255, 101.99999999999997, 255);
56+
color: rgb(255, 102, 255);
5757
color: rgb(153, 0, 153);
5858
color: rgb(51, 0, 51);
5959
color: rgb(153, 102, 153);
6060
color: rgb(255, 0, 255);
61-
color: rgb(229.5, 25.499999999999993, 229.5);
61+
color: rgb(230, 25, 230);
6262
color: rgb(153, 102, 153);
6363
}
6464

@@ -74,23 +74,23 @@ test-blackness-whiteness-adjuster {
7474
}
7575

7676
test-tint-shade-adjuster {
77-
color: rgb(50.999999999999986, 0, 0);
77+
color: rgb(51, 0, 0);
7878
color: rgb(255, 204, 204);
7979
}
8080

8181
test-blend-adjuster {
82-
color: rgb(255, 127.5, 0);
83-
color: rgb(255, 127.5, 0);
84-
color: rgb(255, 127.5, 255);
85-
color: rgb(255, 127.5, 0);
82+
color: rgb(255, 128, 0);
83+
color: rgb(255, 128, 0);
84+
color: rgb(255, 128, 255);
85+
color: rgb(255, 128, 0);
8686
}
8787

8888
test-contrast-adjuster {
89-
color: rgb(191.25, 191.25, 0);
90-
color: rgb(127.5, 127.5, 0);
91-
color: rgb(63.75, 63.75, 0);
89+
color: rgb(191, 191, 0);
90+
color: rgb(128, 128, 0);
91+
color: rgb(64, 64, 0);
9292
}
9393

9494
test-combination-adjuster {
95-
color: rgb(88.14750382701874, 45.50833333333334, 113.64750382701874);
95+
color: rgb(88, 46, 114);
9696
}

0 commit comments

Comments
 (0)