-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsColorComparer.js
More file actions
147 lines (121 loc) · 4.1 KB
/
jsColorComparer.js
File metadata and controls
147 lines (121 loc) · 4.1 KB
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
jsColorComparerTypes = {
RGB: function (red, green, blue) {
this.r = red;
this.g = green;
this.b = blue;
this.toString = function() {
return 'RGB (' + this.r + ', ' + this.g + ', ' + this.b + ')';
};
},
HSL: function (hue, saturation, lightness) {
this.h = hue;
this.s = saturation;
this.l = lightness;
this.toString = function() {
return 'HSL (' + Math.round(this.h * 60) + '%, ' + Math.round(this.s * 100) + '%, ' + Math.round(this.l * 100) + '%)';
};
},
Hex: function (hex) {
this.r = hex[1] + hex[2];
this.g = hex[3] + hex[4];
this.b = hex[5] + hex[6];
this.toString = function() {
return 'Hex (' + this.r + ', ' + this.g + ', ' + this.b + ')';
};
}
};
var jsColorComparerTools = {
/*Constants*/
/*******************************/
hexLength: 7,
colorRange: {min: 0, max: 255},
/*Color types conversions*/
/******************************/
hexToRgb: function (hex) {
if (this.isHexFormat(hex)) {
var hexObj = new jsColorComparerTypes.Hex(hex);
return new jsColorComparerTypes.RGB(this.hexToDec(hexObj.r), this.hexToDec(hexObj.g), this.hexToDec(hexObj.b));
}
},
rgbToHsl: function (rgb) {
if (rgb instanceof jsColorComparerTypes.RGB) {
var h, s, l;
var newR = rgb.r / this.colorRange.max;
var newG = rgb.g / this.colorRange.max;
var newB = rgb.b / this.colorRange.max;
var max = Math.max(newR, newG, newB);
var min = Math.min(newR, newG, newB);
var delta = max - min;
l = (max + min) / 2;
if (delta == 0) {
h = s = 0;
} else {
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
switch (max) {
case newR:
h = ((newG - newB) / delta) % 6;
break;
case newG:
h = (newB - newR) / delta + 2;
break;
case newB:
h = (newR - newG) / delta + 4;
default:
break;
}
}
return new jsColorComparerTypes.HSL(h, s, l);
}
},
toHsl: function (color) {
if (this.isHexFormat(color)) {
return this.rgbToHsl(this.hexToRgb(color));
} else if (this.isRgbFormat(color)) {
return this.rgbToHsl(new jsColorComparerTypes.RGB(color.r, color.g, color.b));
}
},
/*Additional methods*/
/******************************/
hexToDec: function (hex) {
return parseInt(hex, 16);
},
isHexFormat: function (color) {
return /#([0-9]|[A-F]){6}/gi.test(color);
},
isRgbFormat: function (color) {
return color.r != null && color.g != null && color.b != null;
},
isHslFormat: function (color) {
return color.h != null && color.s != null && color.l != null;
}
};
/*jsColorComparer*/
/******************************/
var jsColorComparer = {
whichIsDarker: function(colorArray) {
var darker = colorArray[0];
var lightness = jsColorComparerTools.toHsl(darker).l;
var currentHsl;
for(var i = 1; i < colorArray.length; i++) {
currentHsl = jsColorComparerTools.toHsl(colorArray[i]);
if (currentHsl.l < lightness) {
darker = colorArray[i];
lightness = currentHsl.l;
}
}
return darker;
},
whichIsLighter: function(colorArray) {
var lighter = colorArray[0];
var lightness = jsColorComparerTools.toHsl(lighter).l;
var currentHsl;
for(var i = 1; i < colorArray.length; i++) {
currentHsl = jsColorComparerTools.toHsl(colorArray[i]);
if (currentHsl.l > lightness) {
lighter = colorArray[i];
lightness = currentHsl.l;
}
}
return lighter;
},
};