-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
122 lines (111 loc) · 2.39 KB
/
colors.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
var convert = require('color-convert')
module.exports = {
Rgb: Rgb,
Rgba: Rgba,
Hsl: Hsl,
Hsla: Hsla,
toHsl: toHsl,
fromHsl: fromHsl
}
function hexDigit(number) {
var c = '0123456789abcdef'
return c[parseInt(number/16)] + c[parseInt(number) % 16]
}
function Rgb(r, g, b) {
return {
r:r,
g:g,
b:b,
toList: function () {
return [this.r, this.g, this.b]
},
toString: function () {
return 'rgb(' + parseInt(this.r) + ', ' + parseInt(this.g) + ', ' + parseInt(this.b) + ')'
},
toHex: function () {
return '#' + this.toList().map(hexDigit).join('')
}
}
}
function Rgba(r, g, b, a) {
return {
r: r,
g: g,
b: b,
a: a,
toList: function () {
return [this.r, this.g, this.b, this.a]
},
toString: function () {
var vars = this.toList()
vars[3] *= 100
vars = vars.map(parseInt)
vars[3] /= 100
return 'rgba(' + parseInt(this.r) + ', ' + parseInt(this.g) + ', ' + parseInt(this.b) + ', ' + parseInt(this.a*100)/100 + ')'
},
// the alpha just gets dropped
toHex: function () {
var items = this.toList().slice(0, 3)
return '#' + items.map(hexDigit).join('')
}
}
}
function Hsl(h, s, l) {
return {
h: h,
s: s,
l: l,
toList: function () {
return [this.h, this.s, this.l]
},
toString: function () {
return 'hsl(' + parseInt(this.h) + ', ' + parseInt(this.s) + '%, ' + parseInt(this.l) + '%)'
}
}
}
function Hsla(h, s, l, a) {
return {
h: h,
s: s,
l: l,
a: a,
toList: function () {
return [this.h, this.s, this.l, this.a]
},
toString: function () {
return 'hsla(' + parseInt(this.h) + ', ' + parseInt(this.s) + '%, ' + parseInt(this.l) + '%, ' + parseInt(this.a * 100)/100 + ')'
}
}
}
function toHsl(color) {
if (!color) return
var res
if (convert.rgb2hsl) {
res = convert.rgb2hsl(color.r, color.g, color.b)
res[0] /= 360
res[1] /= 100
res[2] /= 100
} else {
res = convert.RGBtoHSL(color.r, color.g, color.b)
}
return {
h: res[0],
s: res[1],
l: res[2],
a: color.a
}
}
function fromHsl(color) {
var rgb
if (convert.hsl2rgb) {
rgb = convert.hsl2rgb(color.h * 360, color.s * 100, color.l * 100)
} else {
rgb = convert.HSLtoRGB(color.h, color.s, color.l)
}
return {
r: rgb[0],
g: rgb[1],
b: rgb[2],
a: color.a
}
}