@tryghost/color-utils 0.1.27 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/cjs/color-utils.js +8 -25
- package/es/color-utils.js +28 -49
- package/es/color-utils.js.map +1 -1
- package/package.json +12 -8
- package/{lib/color-utils.js → src/color-utils.ts} +3 -3
- package/types/color-utils.d.ts +5 -0
- package/umd/color-utils.min.js.map +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
package/cjs/color-utils.js
CHANGED
|
@@ -7,54 +7,37 @@ var Color = require('color');
|
|
|
7
7
|
function lightenToContrastThreshold(foreground, background, contrastThreshold) {
|
|
8
8
|
const foregroundColor = Color(foreground);
|
|
9
9
|
const backgroundColor = Color(background);
|
|
10
|
-
|
|
11
|
-
const {h,s} = foregroundColor.hsl().object();
|
|
12
|
-
|
|
10
|
+
const { h, s } = foregroundColor.hsl().object();
|
|
13
11
|
let newColor = foregroundColor;
|
|
14
|
-
|
|
15
12
|
while (newColor.contrast(backgroundColor) < contrastThreshold) {
|
|
16
13
|
if (newColor.lightness() >= 100) {
|
|
17
14
|
break;
|
|
18
15
|
}
|
|
19
|
-
|
|
20
|
-
newColor = Color({h, s, l: newColor.lightness() + 5});
|
|
16
|
+
newColor = Color({ h, s, l: newColor.lightness() + 5 });
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
return newColor;
|
|
24
19
|
}
|
|
25
|
-
|
|
26
20
|
function darkenToContrastThreshold(foreground, background, contrastThreshold) {
|
|
27
21
|
const foregroundColor = Color(foreground);
|
|
28
22
|
const backgroundColor = Color(background);
|
|
29
|
-
|
|
30
|
-
const {h,s} = foregroundColor.hsl().object();
|
|
31
|
-
|
|
23
|
+
const { h, s } = foregroundColor.hsl().object();
|
|
32
24
|
let newColor = foregroundColor;
|
|
33
|
-
|
|
34
25
|
while (newColor.contrast(backgroundColor) < contrastThreshold) {
|
|
35
26
|
if (newColor.lightness() <= 0) {
|
|
36
27
|
break;
|
|
37
28
|
}
|
|
38
|
-
|
|
39
|
-
newColor = Color({h, s, l: newColor.lightness() - 5});
|
|
29
|
+
newColor = Color({ h, s, l: newColor.lightness() - 5 });
|
|
40
30
|
}
|
|
41
|
-
|
|
42
31
|
return newColor;
|
|
43
32
|
}
|
|
44
|
-
|
|
45
33
|
function textColorForBackgroundColor(background) {
|
|
46
34
|
const backgroundColor = Color(background);
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const black = Color({r: 0, g: 0, b: 0});
|
|
50
|
-
|
|
35
|
+
const white = Color({ r: 255, g: 255, b: 255 });
|
|
36
|
+
const black = Color({ r: 0, g: 0, b: 0 });
|
|
51
37
|
// shared with Portal https://github.com/TryGhost/Portal/blob/317876f20d22431df15e655ea6cc197fe636615e/src/utils/contrast-color.js#L26-L29
|
|
52
|
-
const yiq = (
|
|
53
|
-
backgroundColor.red() * 0.299 +
|
|
38
|
+
const yiq = (backgroundColor.red() * 0.299 +
|
|
54
39
|
backgroundColor.green() * 0.587 +
|
|
55
|
-
backgroundColor.b() * 0.114
|
|
56
|
-
);
|
|
57
|
-
|
|
40
|
+
backgroundColor.b() * 0.114);
|
|
58
41
|
return (yiq >= 186) ? black : white;
|
|
59
42
|
}
|
|
60
43
|
|
package/es/color-utils.js
CHANGED
|
@@ -2128,61 +2128,40 @@ function zeroArray(arr, length) {
|
|
|
2128
2128
|
var color = Color;
|
|
2129
2129
|
|
|
2130
2130
|
function lightenToContrastThreshold(foreground, background, contrastThreshold) {
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
break;
|
|
2131
|
+
const foregroundColor = color(foreground);
|
|
2132
|
+
const backgroundColor = color(background);
|
|
2133
|
+
const { h, s } = foregroundColor.hsl().object();
|
|
2134
|
+
let newColor = foregroundColor;
|
|
2135
|
+
while (newColor.contrast(backgroundColor) < contrastThreshold) {
|
|
2136
|
+
if (newColor.lightness() >= 100) {
|
|
2137
|
+
break;
|
|
2138
|
+
}
|
|
2139
|
+
newColor = color({ h, s, l: newColor.lightness() + 5 });
|
|
2141
2140
|
}
|
|
2142
|
-
newColor
|
|
2143
|
-
h,
|
|
2144
|
-
s,
|
|
2145
|
-
l: newColor.lightness() + 5
|
|
2146
|
-
});
|
|
2147
|
-
}
|
|
2148
|
-
return newColor;
|
|
2141
|
+
return newColor;
|
|
2149
2142
|
}
|
|
2150
2143
|
function darkenToContrastThreshold(foreground, background, contrastThreshold) {
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
break;
|
|
2144
|
+
const foregroundColor = color(foreground);
|
|
2145
|
+
const backgroundColor = color(background);
|
|
2146
|
+
const { h, s } = foregroundColor.hsl().object();
|
|
2147
|
+
let newColor = foregroundColor;
|
|
2148
|
+
while (newColor.contrast(backgroundColor) < contrastThreshold) {
|
|
2149
|
+
if (newColor.lightness() <= 0) {
|
|
2150
|
+
break;
|
|
2151
|
+
}
|
|
2152
|
+
newColor = color({ h, s, l: newColor.lightness() - 5 });
|
|
2161
2153
|
}
|
|
2162
|
-
newColor
|
|
2163
|
-
h,
|
|
2164
|
-
s,
|
|
2165
|
-
l: newColor.lightness() - 5
|
|
2166
|
-
});
|
|
2167
|
-
}
|
|
2168
|
-
return newColor;
|
|
2154
|
+
return newColor;
|
|
2169
2155
|
}
|
|
2170
2156
|
function textColorForBackgroundColor(background) {
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
r:
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
g: 0,
|
|
2180
|
-
b: 0
|
|
2181
|
-
});
|
|
2182
|
-
|
|
2183
|
-
// shared with Portal https://github.com/TryGhost/Portal/blob/317876f20d22431df15e655ea6cc197fe636615e/src/utils/contrast-color.js#L26-L29
|
|
2184
|
-
const yiq = backgroundColor.red() * 0.299 + backgroundColor.green() * 0.587 + backgroundColor.b() * 0.114;
|
|
2185
|
-
return yiq >= 186 ? black : white;
|
|
2157
|
+
const backgroundColor = color(background);
|
|
2158
|
+
const white = color({ r: 255, g: 255, b: 255 });
|
|
2159
|
+
const black = color({ r: 0, g: 0, b: 0 });
|
|
2160
|
+
// shared with Portal https://github.com/TryGhost/Portal/blob/317876f20d22431df15e655ea6cc197fe636615e/src/utils/contrast-color.js#L26-L29
|
|
2161
|
+
const yiq = (backgroundColor.red() * 0.299 +
|
|
2162
|
+
backgroundColor.green() * 0.587 +
|
|
2163
|
+
backgroundColor.b() * 0.114);
|
|
2164
|
+
return (yiq >= 186) ? black : white;
|
|
2186
2165
|
}
|
|
2187
2166
|
|
|
2188
2167
|
export { color as Color, darkenToContrastThreshold, lightenToContrastThreshold, textColorForBackgroundColor };
|