Skip to content

Commit 6a65e9f

Browse files
authored
Merge pull request #18 from ceoss/lower-rgb
Allow lowercase rgb objects, test only with deepStrictEquals
2 parents 3a2b977 + cadc9ba commit 6a65e9f

File tree

5 files changed

+144
-82
lines changed

5 files changed

+144
-82
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Returns a mapping from the colors in palette1 to palette2.
6868
#### color
6969
`Object`
7070

71-
`color` is an object containing 3 properties: 'R', 'G', 'B', such as:
71+
`color` is an object containing 3 properties: 'R', 'G', 'B' (case insensitive), such as:
7272

7373
```js
7474
{ R: 255, G: 1, B: 0 }

lib/convert.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*/
3232
exports.rgb_to_lab = rgb_to_lab;
3333
exports.rgba_to_lab = rgba_to_lab;
34+
exports.normalize_rgb = normalize_rgb;
3435

3536
/**
3637
* IMPORTS
@@ -45,13 +46,14 @@ var sqrt = Math.sqrt;
4546
/**
4647
* Returns c converted to labcolor. Uses bc as background color,
4748
* deaults to using white as background color.
48-
* @param {rgbacolor} c should have fields R,G,B,A
49-
* @param {rgbcolor} b should have fields R,G,B
49+
* @param {rgbacolor} c should have fields R,G,B,A (case insensitive)
50+
* @param {rgbcolor} b should have fields R,G,B (case insensitive)
5051
* @return {labcolor} c converted to labcolor
5152
*/
5253
function rgba_to_lab(c, bc)
5354
{
54-
var bc = typeof bc !== 'undefined' ? bc : {R: 255, G: 255, B:255};
55+
c = normalize_rgb(c);
56+
var bc = typeof bc !== 'undefined' ? normalize_rgb(bc) : {R: 255, G: 255, B:255};
5557
var new_c = {R: bc.R + (c.R - bc.R) * c.A,
5658
G: bc.G + (c.G - bc.G) * c.A,
5759
B: bc.B + (c.B - bc.B) * c.A};
@@ -60,7 +62,7 @@ function rgba_to_lab(c, bc)
6062

6163
/**
6264
* Returns c converted to labcolor.
63-
* @param {rgbcolor} c should have fields R,G,B
65+
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
6466
* @return {labcolor} c converted to labcolor
6567
*/
6668
function rgb_to_lab(c)
@@ -69,12 +71,13 @@ function rgb_to_lab(c)
6971
}
7072

7173
/**
72-
* Returns c converted to xyzcolor.
73-
* @param {rgbcolor} c should have fields R,G,B
74-
* @return {xyzcolor} c converted to xyzcolor
75-
*/
74+
* Returns c converted to xyzcolor.
75+
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
76+
* @return {xyzcolor} c converted to xyzcolor
77+
*/
7678
function rgb_to_xyz(c)
7779
{
80+
c = normalize_rgb(c);
7881
// Based on http://www.easyrgb.com/index.php?X=MATH&H=02
7982
var R = ( c.R / 255 );
8083
var G = ( c.G / 255 );
@@ -124,6 +127,22 @@ function xyz_to_lab(c)
124127
return {'L' : L , 'a' : a, 'b' : b};
125128
}
126129

130+
/**
131+
* Returns c converted to uppercase property names (RGBA from rgba).
132+
* @param {(rgbcolor|rgbacolor)} c should have fields R,G,B, can have field A (case insensitive)
133+
* @return {(rgbcolor|rgbacolor)} c convertered to uppercase property names
134+
*/
135+
function normalize_rgb(c)
136+
{
137+
var new_c = {R: c.R || c.r || 0,
138+
G: c.G || c.g || 0,
139+
B: c.B || c.b || 0};
140+
if (typeof c.a !== "undefined" || typeof c.A !== "undefined") {
141+
new_c.A = c.A || c.a || 0;
142+
}
143+
return new_c;
144+
}
145+
127146
// Local Variables:
128147
// allout-layout: t
129148
// js-indent-level: 2

lib/palette.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ var color_convert = require('./convert');
4747

4848
/**
4949
* Returns the hash key used for a {rgbcolor} in a {palettemap}
50-
* @param {rgbcolor} c should have fields R,G,B
50+
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
5151
* @return {string}
5252
*/
5353
function palette_map_key(c)
5454
{
55+
c = color_convert.normalize_rgb(c);
5556
var s = "R" + c.R + "B" + c.B + "G" + c.G;
5657
if ("A" in c) {
5758
s = s + "A" + c.A;
@@ -71,10 +72,10 @@ function lab_palette_map_key(c)
7172

7273
/**
7374
* Returns a mapping from each color in a to the closest/farthest color in b
74-
* @param [{rgbcolor}] a each element should have fields R,G,B
75-
* @param [{rgbcolor}] b each element should have fields R,G,B
76-
* @param {rgbcolor} Optional background color when using alpha channels
77-
* @param 'type' should be the string 'closest' or 'furthest'
75+
* @param [{rgbcolor}] a each element should have fields R,G,B (case insensitive)
76+
* @param [{rgbcolor}] b each element should have fields R,G,B (case insensitive)
77+
* @param {('closest'|'furthest')} type should be the string 'closest' or 'furthest'
78+
* @param {rgbcolor} bc Optional background color when using alpha channels
7879
* @return {palettemap}
7980
*/
8081
function map_palette(a, b, type, bc)
@@ -143,7 +144,7 @@ function match_palette_lab(target_color, palette, find_furthest)
143144
* Returns a mapping from each color in a to the closest color in b
144145
* @param [{labcolor}] a each element should have fields L,a,b
145146
* @param [{labcolor}] b each element should have fields L,a,b
146-
* @param 'type' should be the string 'closest' or 'furthest'
147+
* @param {('closest'|'furthest')} type should be the string 'closest' or 'furthest'
147148
* @return {labpalettemap}
148149
*/
149150
function map_palette_lab(a, b, type)

test/convert.js

+50-27
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,66 @@ var color_convert = require('../lib/convert');
3939
describe('convert', function(){
4040
describe('#rgb_to_lab()', function(){
4141
it('should convert to expected lab color #1', function(){
42-
assert.deepEqual({'L' : 40.473, 'a' : -6.106, 'b' : -21.417},
43-
round_all(color_convert.rgb_to_lab({'R' : 55,
44-
'G' : 100,
45-
'B' : 130})));
42+
assert.deepStrictEqual({'L' : 40.473, 'a' : -6.106, 'b' : -21.417},
43+
round_all(color_convert.rgb_to_lab({'R' : 55,
44+
'G' : 100,
45+
'B' : 130})));
4646
});
4747
it('should convert to expected lab color #2', function(){
48-
assert.deepEqual({'L' : 0, 'a' : 0, 'b' : 0},
49-
round_all(color_convert.rgb_to_lab({'R' : 0,
50-
'G' : 0,
51-
'B' : 0})));
48+
assert.deepStrictEqual({'L' : 0, 'a' : 0, 'b' : 0},
49+
round_all(color_convert.rgb_to_lab({'R' : 0,
50+
'G' : 0,
51+
'B' : 0})));
5252
});
5353
it('should convert to expected lab color #3', function(){
54-
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
55-
round_all(color_convert.rgb_to_lab({'R' : 255,
56-
'G' : 255,
57-
'B' : 255})));
54+
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
55+
round_all(color_convert.rgb_to_lab({'R' : 255,
56+
'G' : 255,
57+
'B' : 255})));
5858
});
5959
it('should convert to expected lab color #4', function(){
60-
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
61-
round_all(color_convert.rgba_to_lab({'R' : 255,
62-
'G' : 255,
63-
'B' : 255,
64-
'A' : 1.0})));
60+
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
61+
round_all(color_convert.rgba_to_lab({'R' : 255,
62+
'G' : 255,
63+
'B' : 255,
64+
'A' : 1.0})));
6565
});
6666
it('should convert to expected lab color #5', function(){
67-
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
68-
round_all(color_convert.rgba_to_lab({'R' : 0,
69-
'G' : 0,
70-
'B' : 0,
71-
'A' : 0.0})));
67+
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
68+
round_all(color_convert.rgba_to_lab({'R' : 0,
69+
'G' : 0,
70+
'B' : 0,
71+
'A' : 0.0})));
7272
});
7373
it('should convert to expected lab color #6', function(){
74-
assert.deepEqual({"L": 53.389, "a": 0.003, "b": -0.006},
75-
round_all(color_convert.rgba_to_lab({'R' : 0,
76-
'G' : 0,
77-
'B' : 0,
78-
'A' : 0.5})));
74+
assert.deepStrictEqual({"L": 53.389, "a": 0.003, "b": -0.006},
75+
round_all(color_convert.rgba_to_lab({'R' : 0,
76+
'G' : 0,
77+
'B' : 0,
78+
'A' : 0.5})));
79+
});
80+
it('should convert to expected lab color #6 from lowercase RGBA object', function(){
81+
assert.deepStrictEqual({"L": 53.389, "a": 0.003, "b": -0.006},
82+
round_all(color_convert.rgba_to_lab({'r' : 0,
83+
'g' : 0,
84+
'b' : 0,
85+
'a' : 0.5})));
86+
});
87+
})
88+
89+
describe('#normalize_rgb()', function () {
90+
it('should convert lowercase RGB props to uppercase', function () {
91+
assert.deepStrictEqual({'R': 55, 'G': 255, 'B': 0},
92+
color_convert.normalize_rgb({'r': 55,
93+
'g': 255,
94+
'b': 0}));
95+
});
96+
it('should convert lowercase RGBA props to uppercase', function () {
97+
assert.deepStrictEqual({'R': 55, 'G': 255, 'B': 0, 'A': 0},
98+
color_convert.normalize_rgb({'r': 55,
99+
'g': 255,
100+
'b': 0,
101+
'a': 0}));
79102
});
80103
})
81104
});

0 commit comments

Comments
 (0)