forked from siege-media/contrast-ratio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.js
177 lines (135 loc) · 3.65 KB
/
color.js
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
// Extend Math.round to allow for precision
Math.round = (function(){
var round = Math.round;
return function (number, decimals) {
decimals = +decimals || 0;
var multiplier = Math.pow(10, decimals);
return round(number * multiplier) / multiplier;
};
})();
// Simple class for handling sRGB colors
(function(){
var _ = self.Color = function(rgba) {
if (rgba === 'transparent') {
rgba = [0,0,0,0];
}
else if (typeof rgba === 'string') {
var rgbaString = rgba;
rgba = rgbaString.match(/rgba?\(([\d.]+), ([\d.]+), ([\d.]+)(?:, ([\d.]+))?\)/);
if (rgba) {
rgba.shift();
}
else {
throw new Error('Invalid string: ' + rgbaString);
}
}
if (rgba[3] === undefined) {
rgba[3] = 1;
}
rgba = rgba.map(function (a) { return Math.round(a, 3) });
this.rgba = rgba;
};
_.prototype = {
get rgb () {
return this.rgba.slice(0,3);
},
get alpha () {
return this.rgba[3];
},
set alpha (alpha) {
this.rgba[3] = alpha;
},
get luminance () {
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
var rgba = this.rgba.slice();
for(var i=0; i<3; i++) {
var rgb = rgba[i];
rgb /= 255;
rgb = rgb < .03928 ? rgb / 12.92 : Math.pow((rgb + .055) / 1.055, 2.4);
rgba[i] = rgb;
}
return .2126 * rgba[0] + .7152 * rgba[1] + 0.0722 * rgba[2];
},
get inverse () {
return new _([
255 - this.rgba[0],
255 - this.rgba[1],
255 - this.rgba[2],
this.alpha
]);
},
toString: function() {
return 'rgb' + (this.alpha < 1? 'a' : '') + '(' + this.rgba.slice(0, this.alpha >= 1? 3 : 4).join(', ') + ')';
},
clone: function() {
return new _(this.rgba);
},
// Overlay a color over another
overlayOn: function (color) {
var overlaid = this.clone();
var alpha = this.alpha;
if (alpha >= 1) {
return overlaid;
}
for(var i=0; i<3; i++) {
overlaid.rgba[i] = overlaid.rgba[i] * alpha + color.rgba[i] * color.rgba[3] * (1 - alpha);
}
overlaid.rgba[3] = alpha + color.rgba[3] * (1 - alpha);
return overlaid;
},
contrast: function (color) {
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
var alpha = this.alpha;
if (alpha >= 1) {
if (color.alpha < 1) {
color = color.overlayOn(this);
}
var l1 = this.luminance + .05,
l2 = color.luminance + .05,
ratio = l1/l2;
if (l2 > l1) {
ratio = 1 / ratio;
}
ratio = Math.round(ratio, 1);
return {
ratio: ratio,
error: 0,
min: ratio,
max: ratio
};
}
// If we’re here, it means we have a semi-transparent background
// The text color may or may not be semi-transparent, but that doesn't matter
var onBlack = this.overlayOn(_.BLACK),
onWhite = this.overlayOn(_.WHITE),
contrastOnBlack = onBlack.contrast(color).ratio,
contrastOnWhite = onWhite.contrast(color).ratio;
var max = Math.max(contrastOnBlack, contrastOnWhite);
// This is here for backwards compatibility and not used to calculate
// `min`. Note that there may be other colors with a closer luminance to
// `color` if they have a different hue than `this`.
var closest = this.rgb.map(function(c, i) {
return Math.min(Math.max(0, (color.rgb[i] - c * alpha)/(1-alpha)), 255);
});
closest = new _(closest);
var min = 1;
if (onBlack.luminance > color.luminance) {
min = contrastOnBlack;
}
else if (onWhite.luminance < color.luminance) {
min = contrastOnWhite;
}
return {
ratio: Math.round((min + max) / 2, 2),
error: Math.round((max - min) / 2, 2),
min: min,
max: max,
closest: closest,
farthest: onWhite == max? _.WHITE : _.BLACK
};
}
};
_.BLACK = new _([0,0,0]);
_.GRAY = new _([127.5, 127.5, 127.5]);
_.WHITE = new _([255,255,255]);
})();