Skip to content

Commit c0cecf2

Browse files
authored
Report fully transparent colors as aliases (#65)
`rgba(0,0,0,0)` is visually the same as `rgba(255,255,255,0)`, so let's report them as aliases Closes #63
1 parent 74a3042 commit c0cecf2

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/analyzer/values/colors.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ const addShortestNotation = color => {
6868
}
6969

7070
const addAliases = (acc, curr) => {
71-
if (!acc[curr.key]) {
72-
acc[curr.key] = {
71+
if (!acc[curr.normalized]) {
72+
acc[curr.normalized] = {
7373
aliases: []
7474
}
7575
}
7676

77-
acc[curr.key] = {
78-
aliases: [...acc[curr.key].aliases, curr]
77+
acc[curr.normalized] = {
78+
aliases: [...acc[curr.normalized].aliases, curr]
7979
}
8080

8181
return acc
@@ -96,10 +96,11 @@ const normalizeColors = color => {
9696
// Avoid using TinyColor's toHslString() because it rounds
9797
// the numbers and incorrectly reports aliases
9898
const {h, s, l, a} = tinycolor(color.value).toHsl()
99+
const normalized = a === 0 ? 0 : `h${h}s${s}l${l}a${a}`
99100

100101
return {
101102
...color,
102-
key: `h${h}s${s}l${l}a${a}`
103+
normalized
103104
}
104105
}
105106

@@ -108,7 +109,7 @@ const rmTmpProps = color => {
108109
return {
109110
...color,
110111
aliases: color.aliases.map(alias => {
111-
const {key, ...restAlias} = alias
112+
const {normalized, ...restAlias} = alias
112113
return restAlias
113114
})
114115
}

test/analyzer/values/input.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
.color-hex {
2727
color: #aff034;
2828
border: 1px solid #aaa;
29-
color: #0000ff00; /* Including opacity */
29+
color: #0000ffaa; /* Including opacity */
3030
}
3131
.color-rgb {
3232
color: rgb(100, 200, 10);
@@ -59,12 +59,16 @@
5959
color: #000;
6060
color: #000000;
6161
color: black;
62-
color: black; /* duplicate */
62+
color: black;
6363
color: rgb(0,0,0);
6464
color: rgba(0,0,0,1);
6565
color: hsl(0,0,0);
6666
color: hsla(0,0,0,1);
6767

68+
/* Fully transparent aliases */
69+
color: rgba(0,0,0,0);
70+
color: hsla(0,0%,0%,0);
71+
6872
/* Almost duplicates */
6973
color: #d9d9d9;
7074
color: #dadada;

test/analyzer/values/output.json

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"total": 90,
2+
"total": 92,
33
"fontfamilies": {
44
"total": 18,
55
"totalUnique": 12,
@@ -121,11 +121,11 @@
121121
"count": 1
122122
}
123123
],
124-
"share": 0.044444444444444446
124+
"share": 0.043478260869565216
125125
},
126126
"colors": {
127-
"total": 36,
128-
"totalUnique": 34,
127+
"total": 38,
128+
"totalUnique": 36,
129129
"unique": [
130130
{
131131
"value": "tomato",
@@ -136,11 +136,11 @@
136136
"count": 1
137137
},
138138
{
139-
"value": "rgb(100, 200, 10)",
139+
"value": "rgba(100, 200, 10, 0.5)",
140140
"count": 1
141141
},
142142
{
143-
"value": "rgba(100, 200, 10, 0.5)",
143+
"value": "rgb(100, 200, 10)",
144144
"count": 1
145145
},
146146
{
@@ -164,25 +164,29 @@
164164
"count": 1
165165
},
166166
{
167-
"value": "hsl(270, 60%, 70%)",
167+
"value": "#0000ffaa",
168168
"count": 1
169169
},
170170
{
171-
"value": "hsl(270,60%,70%)",
171+
"value": "hsl(270 60% 70%)",
172172
"count": 1
173173
},
174174
{
175-
"value": "hsl(270 60% 70%)",
175+
"value": "hsl(270, 60%, 70%)",
176176
"count": 1
177177
},
178178
{
179-
"value": "hsl(270 60% 50% / 15%)",
179+
"value": "hsl(270,60%,70%)",
180180
"count": 1
181181
},
182182
{
183183
"value": "hsl(270 60% 50% / .15)",
184184
"count": 1
185185
},
186+
{
187+
"value": "hsl(270 60% 50% / 15%)",
188+
"count": 1
189+
},
186190
{
187191
"value": "hsl(270, 60%, 50%, .15)",
188192
"count": 1
@@ -260,11 +264,29 @@
260264
"count": 1
261265
},
262266
{
263-
"value": "#0000ff00",
267+
"value": "rgba(0,0,0,0)",
268+
"count": 1
269+
},
270+
{
271+
"value": "hsla(0,0%,0%,0)",
264272
"count": 1
265273
}
266274
],
267275
"duplicates": [
276+
{
277+
"count": 2,
278+
"value": "rgba(0,0,0,0)",
279+
"aliases": [
280+
{
281+
"value": "rgba(0,0,0,0)",
282+
"count": 1
283+
},
284+
{
285+
"value": "hsla(0,0%,0%,0)",
286+
"count": 1
287+
}
288+
]
289+
},
268290
{
269291
"value": "rgba(100, 200, 10, .5)",
270292
"count": 2,
@@ -280,34 +302,34 @@
280302
]
281303
},
282304
{
283-
"value": "hsl(270,60%,70%)",
305+
"value": "hsl(270 60% 70%)",
284306
"count": 3,
285307
"aliases": [
286308
{
287309
"count": 1,
288-
"value": "hsl(270, 60%, 70%)"
310+
"value": "hsl(270 60% 70%)"
289311
},
290312
{
291313
"count": 1,
292-
"value": "hsl(270,60%,70%)"
314+
"value": "hsl(270, 60%, 70%)"
293315
},
294316
{
295317
"count": 1,
296-
"value": "hsl(270 60% 70%)"
318+
"value": "hsl(270,60%,70%)"
297319
}
298320
]
299321
},
300322
{
301-
"value": "hsl(270 60% 50% / 15%)",
323+
"value": "hsl(270 60% 50% / .15)",
302324
"count": 4,
303325
"aliases": [
304326
{
305327
"count": 1,
306-
"value": "hsl(270 60% 50% / 15%)"
328+
"value": "hsl(270 60% 50% / .15)"
307329
},
308330
{
309331
"count": 1,
310-
"value": "hsl(270 60% 50% / .15)"
332+
"value": "hsl(270 60% 50% / 15%)"
311333
},
312334
{
313335
"count": 1,

0 commit comments

Comments
 (0)