-
Notifications
You must be signed in to change notification settings - Fork 21
/
invert.ts
169 lines (150 loc) · 5.13 KB
/
invert.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
// -------------------------------
// TYPES / INTERFACES
// -------------------------------
/**
* RGB object type with red, green and blue components.
*/
export type RGB = {
r: number;
g: number;
b: number;
}
/**
* RGB list (array) type with red, green and blue components.
*/
export type RgbArray = [number, number, number];
/**
* Hexadecimal representation of a color.
*/
export type HexColor = string;
/**
* Color represented as hexadecimal value or as RGB object or list.
*/
export type Color = RGB | RgbArray | HexColor;
/**
* Interface for defining black and white colors; used to amplify the contrast
* of the color inversion.
*/
export interface BlackWhite {
black: HexColor;
white: HexColor;
threshold?: number
}
// -------------------------------
// CONSTANTS
// -------------------------------
const DEFAULT_THRESHOLD = Math.sqrt(1.05 * 0.05) - 0.05;
const RE_HEX = /^(?:[0-9a-f]{3}){1,2}$/i;
const DEFAULT_BW: BlackWhite = {
black: '#000000',
white: '#ffffff',
threshold: DEFAULT_THRESHOLD
};
// -------------------------------
// HELPER METHODS
// -------------------------------
function padz(str: string, len: number = 2): string {
return (new Array(len).join('0') + str).slice(-len);
}
function hexToRgbArray(hex: string): RgbArray {
if (hex.slice(0, 1) === '#') hex = hex.slice(1);
if (!RE_HEX.test(hex)) throw new Error(`Invalid HEX color: "${hex}"`);
// normalize / convert 3-chars hex to 6-chars.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
return [
parseInt(hex.slice(0, 2), 16), // r
parseInt(hex.slice(2, 4), 16), // g
parseInt(hex.slice(4, 6), 16) // b
];
}
function toRGB(c: RgbArray): RGB {
return { r: c[0], g: c[1], b: c[2] };
}
function toRgbArray(c: Color): RgbArray {
if (!c) throw new Error('Invalid color value');
if (Array.isArray(c)) return c as RgbArray;
return typeof c === 'string' ? hexToRgbArray(c) : [c.r, c.g, c.b];
}
// http://stackoverflow.com/a/3943023/112731
function getLuminance(c: RgbArray): number {
let i, x;
const a = []; // so we don't mutate
for (i = 0; i < c.length; i++) {
x = c[i] / 255;
a[i] = x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
return 0.2126 * a[0] + 0.7152 * a[1] + 0.0722 * a[2];
}
function invertToBW(color, bw: BlackWhite | boolean, asArr?: boolean): RgbArray | HexColor {
const options = (bw === true)
? DEFAULT_BW
: Object.assign({}, DEFAULT_BW, bw);
return getLuminance(color) > options.threshold
? (asArr ? hexToRgbArray(options.black) : options.black)
: (asArr ? hexToRgbArray(options.white) : options.white);
}
// -------------------------------
// PUBLIC MEMBERS
// -------------------------------
/**
* Generates inverted (opposite) version of the given color.
* @param {Color} color - Color to be inverted.
* @param {BlackWhite|boolean} [bw=false] - Whether to amplify the inversion to
* black or white. Provide an object to customize black/white colors.
* @returns {HexColor} - Hexadecimal representation of the inverted color.
*/
function invert(color: Color, bw: BlackWhite | boolean = false): HexColor {
color = toRgbArray(color);
if (bw) return invertToBW(color, bw) as HexColor;
return '#' + color.map(c => padz((255 - c).toString(16))).join('');
}
/**
* Utility methods to generate inverted version of a color.
* @namespace
*/
namespace invert {
/**
* Generates inverted (opposite) version of the given color, as a RGB object.
* @alias invert.asRgbObject
* @param {Color} color - Color to be inverted.
* @param {BlackWhite|boolean} [bw] - Whether to amplify the inversion to
* black or white. Provide an object to customize black/white colors.
* @returns {RGB} - RGB object representation of the inverted color.
*/
export function asRGB(color: Color, bw?: BlackWhite | boolean): RGB {
color = toRgbArray(color);
const list: RgbArray = bw
? invertToBW(color, bw, true) as RgbArray
: color.map(c => 255 - c) as RgbArray;
return toRGB(list);
}
/**
* Generates inverted (opposite) version of the given color, as a RGB array.
* @param {Color} color - Color to be inverted.
* @param {BlackWhite|boolean} [bw] - Whether to amplify the inversion to
* black or white. Provide an object to customize black/white colors.
* @returns {RGB} - RGB array representation of the inverted color.
*/
export function asRgbArray(color: Color, bw?: BlackWhite | boolean): RgbArray {
color = toRgbArray(color);
return bw
? invertToBW(color, bw, true) as RgbArray
: color.map(c => 255 - c) as RgbArray;
}
/**
* Default luminance threshold used for amplifying inversion to black and
* white.
* @type {number}
*/
export const defaultThreshold = DEFAULT_THRESHOLD;
/**
* Alias of `.asRGB()`
*/
export const asRgbObject = asRGB;
}
// -------------------------------
// EXPORT
// -------------------------------
export default invert;