-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversions.ts
242 lines (204 loc) · 6.22 KB
/
conversions.ts
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
export const AAAThreshold: number = 7; // 7:1 for AAA Text
export const AAThreshold: number = 4.5; // 4.5:1 for AA Text
export const largeThreshold: number = 3; // 3:1 for large text (18pt or 14pt bold)
/**
* Convert a string representation of a colour in RGB to an array of three
* values
*
* @param {string} colour
*
* @return {Object} The RGB representation [0..255, 0..255, 0..255]
*/
export function rgbStrToObject(colour: string): RGB {
const rgbArray = colour.match(/#?(..)(..)(..)/);
if(!rgbArray) throw new Error('Empty or invalid string passed to rgbStrToObject');
const [r, g, b] = rgbArray.slice(1, 4).map((part) => parseInt(part, 16));
return { r, g, b };
}
/**
* Convert an object representation of a colour in RGB to a #rrggbb string
*
* @param {Object} The RGB representation [0..255, 0..255, 0..255]
*
* @return {string} colour
*/
export function rgbObjectToStr(colour: RGB): string {
const toHexStr = (dec: number): string => {
const str = dec.toString(16).toLowerCase();
return dec < 16 ? '0' + str : str;
};
const { r, g, b } = colour;
return `#${toHexStr(r)}${toHexStr(g)}${toHexStr(b)}`;
}
/**
* Contrast Ratio = (Lighter + 0.05) / (Darker + 0.05)
*
* @param {Object} One colour
* @param {Object} Other colour
*
* @return {Number} Contrast ratio between them
*/
export function contrastRatio(rgbA: RGB, rgbB: RGB) {
const a = sRGBLuminance(rgbA) + 0.05;
const b = sRGBLuminance(rgbB) + 0.05;
return a > b ? a / b : b / a;
}
/**
* The relative brightness of any point in a colourspace, normalised to 0 for
* darkest black and 1 for lightest white, with a Gamma of 2.4.
*
* Note 1: For the sRGB colourspace, the relative luminance of a colour is defined as
* L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are defined as:
*
* if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4
* if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4
* if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4
*
* and RsRGB, GsRGB, and BsRGB are defined as:
*
* RsRGB = R8bit / 255
* GsRGB = G8bit / 255
* BsRGB = B8bit / 255
*
* I have subsequently discovered
* (here: http://entropymine.com/imageworsener/srgbformula/)
* that the cutoff point 0.03928, used with 12.92, is probably wrong, so I
* am using 0.04045 from now on.
*
* @param {Object} Colour
*
* @return {Number} The luminance
*/
export function sRGBLuminance(colour: RGB): number {
const mapColour = (value: number) =>
value <= 0.04045 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
const r = mapColour(colour.r / 255);
const g = mapColour(colour.g / 255);
const b = mapColour(colour.b / 255);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
/**
* Converts an RGB colour value to HSV. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
*
* @param {Object} Colour
*
* @return {Object} The HSV representation [0..360, 0..100%, 0..100%]
*/
export function RGBtoHSV(colour: RGB): HSV {
const r = colour.r / 255;
const g = colour.g / 255;
const b = colour.b / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const v = max * 100; // V = MAX
const delta = max - min;
const s = max === 0 ? 0 : (delta / max) * 100;
let h = 0;
if (delta !== 0) {
if (max === r) h = (g - b) / delta + (b > g ? 6 : 0);
else if (max === g) h = (b - r) / delta + 2;
else if (max === b) h = (r - g) / delta + 4;
}
h = Math.round(h * 60);
return {
h,
s: Math.round(s),
v: Math.round(v),
};
}
/**
* Converts an RGB colour value to HSL. Conversion formula
* adapted from https://css-tricks.com/converting-color-spaces-in-javascript/
*
* @param {Object} Colour
*
* @return {Object} The HSL representation [0..360, 0..100%, 0..100%]
*/
export function RGBtoHSL(colour: RGB): HSL {
const r = colour.r / 255;
const g = colour.g / 255;
const b = colour.b / 255;
const min = Math.min(r, g, b);
const max = Math.max(r, g, b);
const delta = max - min;
let h = 0;
if (delta !== 0) {
if (max === r) h = ((g - b) / delta) % 6;
else if (max === g) h = (b - r) / delta + 2;
else if (max === b) h = (r - g) / delta + 4;
}
h = Math.round(h * 60);
if (h < 0) h += 360;
const l = (max + min) / 2;
const s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
return {
h: Math.round(h),
s: Math.round(s * 100),
l: Math.round(l * 100),
};
}
export function RGBtoHSL2(colour: RGB): HSL {
const r = colour.r / 255;
const g = colour.g / 255;
const b = colour.b / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let [hue, sat, light] = [NaN, 0, (max + min) / 2];
const d = max - min;
if (d !== 0) {
sat = (light === 0 || light === 1) ? 0 : (max - light) / Math.min(light, 1-light);
switch(max) {
case r: hue = (g - b) / d + (g < b ? 6 : 0);
break;
case g: hue = (b - r) / d + 2;
break;
case b: hue = (r - g) / d + 4;
break;
}
hue = hue * 60;
}
return {
h: Math.round(hue),
s: Math.round(sat * 100),
l: Math.round(light * 100),
};
}
/**
* Converts an HSL colour value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSV_color_space.
*
* @param {Object} Colour
*
* @return {Array} The RGB representation [0..255, 0..255, 0..255]
*/
export function HSLtoRGB(colour: HSL): RGB {
const { h } = colour,
s = colour.s / 100, // % -> 0..1
l = colour.l / 100;
const c = (1 - Math.abs(2 * l - 1)) * s;
const hp = h / 60.0;
const x = c * (1 - Math.abs((hp % 2) - 1));
const m = l - c / 2;
let r, g, b;
if (hp >= 0 && hp < 1) {
[r, g, b] = [c, x, 0];
} else if (hp >= 1 && hp < 2) {
[r, g, b] = [x, c, 0];
} else if (hp >= 2 && hp < 3) {
[r, g, b] = [0, c, x];
} else if (hp >= 3 && hp < 4) {
[r, g, b] = [0, x, c];
} else if (hp >= 4 && hp < 5) {
[r, g, b] = [x, 0, c];
} else if (hp >= 5 && hp < 6) {
[r, g, b] = [c, 0, x];
} else {
[r, g, b] = [0.9, 0.9, 0.9]; // Should never hit this
}
return {
r: Math.round((r + m) * 255),
g: Math.round((g + m) * 255),
b: Math.round((b + m) * 255),
};
}