-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (125 loc) · 3.15 KB
/
index.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
var parser = require('color-parser')
, colors = require('./colors')
module.exports = Color
// Color(color) where color is one of
// [h,s,l]
// [h,s,l,a]
// 'rgb(r,g,b)'
// 'rgba(r,g,b,a)'
// 'hsl(h,s,l)'
// 'hsla(h,s,l,a)'
// '#ABC'
// '#ABACAD'
// '#ABACADAE' (rgba)
// Color(h, s, l[, a]) where h, s, l, and a are between 0 and 1
function Color(color) {
this.h = 0
this.s = 0
this.l = 0
this.a = 1
if (arguments.length > 1) {
this.rgb.apply(this, arguments)
} else if (color) {
this.set(color)
}
}
Color.prototype = {
// set([h,s,l[,a]])
// set([r,g,b[,a]], 'rgb')
// set('rgb(r,g,b)')
// set('rgba(r,g,b,a)')
// set({r:, g:, b:})
// set({r:, g:, b:, a:})
// set('hsl(h,s,l)')
// set('hsla(h,s,l,a)')
// set({h:, s:, l:})
// set({h:, s:, l:, a:})
set: function (color, type) {
type = type || 'hsl'
if (Array.isArray(color)) {
if (type === 'hsl') {
this.h = color[0]
this.s = color[1]
this.l = color[2]
this.a = color.length === 4 ? color[3] : 1
} else {
this.fromRgb(color)
}
} else if ('string' !== typeof color) {
if ('undefined' !== typeof color.r) {
color.a = 'undefined' === typeof color.a ? 1 : color.a
this.fromRgb(color)
} else if ('undefined' !== typeof color.h) {
this.h = color.h
this.s = color.s
this.l = color.l
this.a = 'undefined' !== typeof color.a ? color.a : 1
} else {
throw new Error("Invalid color set: " + color)
}
} else {
color = color.toLowerCase()
var parsed = parser.hsl(color)
if (!parsed) throw new Error('Unrecognized color ' + color)
this.h = parsed.h
this.s = parsed.s
this.l = parsed.l
this.a = 'undefined' !== typeof parsed.a ? parsed.a : this.a
}
return this
},
fromRgb: function (items) {
if (Array.isArray(items)) {
items = {
r: items[0],
g: items[1],
b: items[2],
a: items.length === 4 ? items[3] : 1
}
}
var hsl = colors.toHsl(items)
this.h = hsl.h
this.s = hsl.s
this.l = hsl.l
this.a = hsl.a
},
rgb: function (color) {
if (arguments.length === 0) {
var rgb = colors.fromHsl(this)
return colors.Rgb(rgb.r, rgb.g, rgb.b)
}
if (arguments.length >= 3) {
this.set([].slice.call(arguments), 'rgb')
} else {
this.set(color, 'rgb')
}
return this
},
rgba: function (color) {
if (arguments.length !== 0) {
return this.rgb.apply(this, arguments)
}
var rgb = colors.fromHsl(this)
return colors.Rgba(rgb.r, rgb.g, rgb.b, this.a)
},
hsl: function (color) {
if (arguments.length === 0) {
return colors.Hsl(this.h * 360, this.s * 100, this.l * 100)
}
if (arguments.length >= 3) {
this.set([].slice.call(arguments), 'hsl')
} else {
this.set(color, 'hsl')
}
return this
},
hsla: function (color) {
if (arguments.length !== 0) {
return this.hsl.apply(this, arguments)
}
return colors.Hsla(this.h * 360, this.s * 100, this.l * 100, this.a)
},
toString: function () {
return this.hsla().toString()
}
}