Skip to content

Commit 4ea7796

Browse files
committed
update
1 parent 177b616 commit 4ea7796

7 files changed

+287
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
components
22
build
3+
node_modules

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ components: component.json
88
clean:
99
rm -fr build components template.js
1010

11-
.PHONY: clean
11+
test:
12+
@mocha -R spec
13+
14+
lint:
15+
@jshint --verbose *.json *.js test/*.js
16+
17+
.PHONY: clean test

colors.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
var convert = require('color-convert')
3+
4+
module.exports = {
5+
Rgb: Rgb,
6+
Rgba: Rgba,
7+
Hsl: Hsl,
8+
Hsla: Hsla,
9+
toHsl: toHsl,
10+
fromHsl: fromHsl
11+
}
12+
13+
function Rgb(r, g, b) {
14+
return {
15+
r:r,
16+
g:g,
17+
b:b,
18+
toList: function () {
19+
return [this.r, this.g, this.b]
20+
},
21+
toString: function () {
22+
return 'rgb(' + this.toList().join(', ') + ')'
23+
}
24+
}
25+
}
26+
27+
function Rgba(r, g, b, a) {
28+
return {
29+
r:r,
30+
g:g,
31+
b:b,
32+
toList: function () {
33+
return [this.r, this.g, this.b, this.a]
34+
},
35+
toString: function () {
36+
return 'rgba(' + this.toList().join(', ') + ')'
37+
}
38+
}
39+
}
40+
41+
function Hsl(h, s, l) {
42+
return {
43+
h: h,
44+
s: s,
45+
l: l,
46+
toList: function () {
47+
return [this.h, this.s, this.l]
48+
},
49+
toString: function () {
50+
return 'hsl(' + this.h + ', ' + this.s + '%, ' + this.l + '%)'
51+
}
52+
}
53+
}
54+
55+
function Hsla(h, s, l, a) {
56+
return {
57+
h: h,
58+
s: s,
59+
l: l,
60+
a: a,
61+
toList: function () {
62+
return [this.h, this.s, this.l, this.a]
63+
},
64+
toString: function () {
65+
return 'hsla(' + this.h + ', ' + this.s + '%, ' + this.l + '%, ' + this.a + ')'
66+
}
67+
}
68+
}
69+
70+
function toHsl(color) {
71+
if (!color) return
72+
var res
73+
if (convert.rgb2hsl) {
74+
res = convert.rgb2hsl(color.r, color.g, color.b)
75+
res[0] /= 360
76+
res[1] /= 100
77+
res[2] /= 100
78+
} else {
79+
res = convert.RGBtoHSL(color.r, color.g, color.b)
80+
}
81+
return {
82+
h: res[0],
83+
s: res[1],
84+
l: res[2],
85+
a: color.a
86+
}
87+
}
88+
89+
function fromHsl(color) {
90+
var rgb
91+
if (convert.hsl2rgb) {
92+
rgb = convert.hsl2rgb(color.h * 360, color.s * 100, color.l * 100)
93+
} else {
94+
rgb = convert.HSLtoRGB(color.h, color.s, color.l)
95+
}
96+
return {
97+
r: rgb[0],
98+
g: rgb[1],
99+
b: rgb[2],
100+
a: color.a
101+
}
102+
}
103+
104+

component.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"description": "A color object that knows about different schemas. Stores as rgb internally.",
55
"version": "0.0.1",
66
"keywords": [],
7-
"dependencies": {},
7+
"dependencies": {
8+
"jazzui/color-parser": "*"
9+
},
810
"development": {},
911
"license": "MIT",
1012
"main": "index.js",
1113
"scripts": [
12-
"index.js"
13-
]
14+
"index.js",
15+
"colors.js"
16+
],
17+
"remotes": []
1418
}

index.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
var parser = require('color-parser')
3+
4+
, colors = require('./colors')
5+
6+
module.exports = Color
7+
8+
// Color(color) where color is one of
9+
// [h,s,l]
10+
// [h,s,l,a]
11+
// 'rgb(r,g,b)'
12+
// 'rgba(r,g,b,a)'
13+
// 'hsl(h,s,l)'
14+
// 'hsla(h,s,l,a)'
15+
// '#ABC'
16+
// '#ABACAD'
17+
// '#ABACADAE' (rgba)
18+
// Color(h, s, l[, a]) where h, s, l, and a are between 0 and 1
19+
function Color(color) {
20+
this.h = 0
21+
this.s = 0
22+
this.l = 0
23+
this.a = 1
24+
if (arguments.length > 1) {
25+
this.rgb.apply(this, arguments)
26+
} else if (color) {
27+
this.set(color)
28+
}
29+
}
30+
31+
Color.prototype = {
32+
// set([h,s,l[,a]])
33+
// set([r,g,b[,a]], 'rgb')
34+
// set('rgb(r,g,b)')
35+
// set('rgba(r,g,b,a)')
36+
// set({r:, g:, b:})
37+
// set({r:, g:, b:, a:})
38+
// set('hsl(h,s,l)')
39+
// set('hsla(h,s,l,a)')
40+
// set({h:, s:, l:})
41+
// set({h:, s:, l:, a:})
42+
set: function (color, type) {
43+
type = type || 'hsl'
44+
if (Array.isArray(color)) {
45+
if (type === 'hsl') {
46+
this.h = color[0]
47+
this.s = color[1]
48+
this.l = color[2]
49+
this.a = color.length === 4 ? color[3] : 1
50+
} else {
51+
this.fromRgb(color)
52+
}
53+
} else if ('string' !== typeof color) {
54+
if ('undefined' !== typeof color.r) {
55+
color.a = 'undefined' === typeof color.a ? 1 : color.a
56+
this.fromRgb(color)
57+
} else if ('undefined' !== typeof color.h) {
58+
this.h = color.h
59+
this.s = color.s
60+
this.l = color.l
61+
this.a = 'undefined' !== typeof color.a ? color.a : 1
62+
} else {
63+
throw new Error("Invalid color set: " + color)
64+
}
65+
} else {
66+
color = color.toLowerCase()
67+
var parsed = parser.hsl(color)
68+
if (!parsed) throw new Error('Unrecognized color ' + color)
69+
this.h = parsed.h
70+
this.s = parsed.s
71+
this.l = parsed.l
72+
this.a = 'undefined' !== typeof parsed.a ? parsed.a : this.a
73+
}
74+
return this
75+
},
76+
fromRgb: function (items) {
77+
if (Array.isArray(items)) {
78+
items = {
79+
r: items[0],
80+
g: items[1],
81+
b: items[2],
82+
a: items.length === 4 ? items[3] : 1
83+
}
84+
}
85+
var hsl = colors.toHsl(items)
86+
this.h = hsl.h
87+
this.s = hsl.s
88+
this.l = hsl.l
89+
this.a = hsl.a
90+
},
91+
rgb: function (color) {
92+
if (arguments.length === 0) {
93+
var rgb = utils.toRgb(this.color)
94+
return colors.Rgb(rgb.r, rgb.g, rgb.b)
95+
}
96+
if (argments.length >= 3) {
97+
this.set([].slice.call(arguments), 'rgb')
98+
}
99+
this.set(color, 'rgb')
100+
},
101+
rgba: function (color) {
102+
if (arguments.length !== 0) {
103+
return this.rgb.apply(this, arguments)
104+
}
105+
var rgb = utils.toRgb(this.color)
106+
return colors.Rgba(rgb.r, rgb.g, rgb.b, this.a)
107+
},
108+
hsl: function (color) {
109+
if (arguments.length === 0) {
110+
return colors.Hsl(this.h * 360, this.s * 100, this.l * 100)
111+
}
112+
if (argments.length >= 3) {
113+
this.set([].slice.call(arguments), 'hsl')
114+
}
115+
this.set(color, 'hsl')
116+
},
117+
hsla: function (color) {
118+
if (arguments.length !== 0) {
119+
return this.hsl.apply(this, arguments)
120+
}
121+
return colors.Hsla(this.h * 360, this.s * 100, this.l * 100, this.a)
122+
},
123+
toString: function () {
124+
return this.hsla().toString()
125+
}
126+
}
127+
128+
129+

package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "color",
3+
"version": "0.0.1",
4+
"scripts": {
5+
"test": "make test"
6+
},
7+
"devDependencies": {
8+
"expect.js": "~0.2.0"
9+
},
10+
"description": "A color object that knows about different schemas. Stores as rgb internally.",
11+
"main": "index.js",
12+
"directories": {
13+
"test": "test"
14+
},
15+
"dependencies": {
16+
"color-convert": "~0.3.1",
17+
"color-parser": "~0.0.1",
18+
"expect.js": "~0.2.0"
19+
},
20+
"author": "Jared Forsyth <[email protected]>",
21+
"license": "Apache v2"
22+
}

test/test_color.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
var expect = require('expect.js')
3+
, Color = require('../')
4+
5+
describe('Color should handle', function () {
6+
var res = 'hsla(340, 100%, 60%, 1)'
7+
it('an array', function () {
8+
expect(new Color([340/360, 1, .6]).toString()).to.equal(res)
9+
})
10+
it('an array w/ alpha', function () {
11+
expect(new Color([340/360, 1, .6, 1]).toString()).to.equal(res)
12+
})
13+
it('an rgb array', function () {
14+
expect(new Color().set([255, 51, 119], 'rgb').toString()).to.equal(res)
15+
})
16+
})
17+

0 commit comments

Comments
 (0)