Skip to content

Commit

Permalink
Merge pull request #18 from ceoss/lower-rgb
Browse files Browse the repository at this point in the history
Allow lowercase rgb objects, test only with deepStrictEquals
  • Loading branch information
markusn authored Feb 23, 2020
2 parents 3a2b977 + cadc9ba commit 6a65e9f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Returns a mapping from the colors in palette1 to palette2.
#### color
`Object`

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

```js
{ R: 255, G: 1, B: 0 }
Expand Down
35 changes: 27 additions & 8 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
exports.rgb_to_lab = rgb_to_lab;
exports.rgba_to_lab = rgba_to_lab;
exports.normalize_rgb = normalize_rgb;

/**
* IMPORTS
Expand All @@ -45,13 +46,14 @@ var sqrt = Math.sqrt;
/**
* Returns c converted to labcolor. Uses bc as background color,
* deaults to using white as background color.
* @param {rgbacolor} c should have fields R,G,B,A
* @param {rgbcolor} b should have fields R,G,B
* @param {rgbacolor} c should have fields R,G,B,A (case insensitive)
* @param {rgbcolor} b should have fields R,G,B (case insensitive)
* @return {labcolor} c converted to labcolor
*/
function rgba_to_lab(c, bc)
{
var bc = typeof bc !== 'undefined' ? bc : {R: 255, G: 255, B:255};
c = normalize_rgb(c);
var bc = typeof bc !== 'undefined' ? normalize_rgb(bc) : {R: 255, G: 255, B:255};
var new_c = {R: bc.R + (c.R - bc.R) * c.A,
G: bc.G + (c.G - bc.G) * c.A,
B: bc.B + (c.B - bc.B) * c.A};
Expand All @@ -60,7 +62,7 @@ function rgba_to_lab(c, bc)

/**
* Returns c converted to labcolor.
* @param {rgbcolor} c should have fields R,G,B
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
* @return {labcolor} c converted to labcolor
*/
function rgb_to_lab(c)
Expand All @@ -69,12 +71,13 @@ function rgb_to_lab(c)
}

/**
* Returns c converted to xyzcolor.
* @param {rgbcolor} c should have fields R,G,B
* @return {xyzcolor} c converted to xyzcolor
*/
* Returns c converted to xyzcolor.
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
* @return {xyzcolor} c converted to xyzcolor
*/
function rgb_to_xyz(c)
{
c = normalize_rgb(c);
// Based on http://www.easyrgb.com/index.php?X=MATH&H=02
var R = ( c.R / 255 );
var G = ( c.G / 255 );
Expand Down Expand Up @@ -124,6 +127,22 @@ function xyz_to_lab(c)
return {'L' : L , 'a' : a, 'b' : b};
}

/**
* Returns c converted to uppercase property names (RGBA from rgba).
* @param {(rgbcolor|rgbacolor)} c should have fields R,G,B, can have field A (case insensitive)
* @return {(rgbcolor|rgbacolor)} c convertered to uppercase property names
*/
function normalize_rgb(c)
{
var new_c = {R: c.R || c.r || 0,
G: c.G || c.g || 0,
B: c.B || c.b || 0};
if (typeof c.a !== "undefined" || typeof c.A !== "undefined") {
new_c.A = c.A || c.a || 0;
}
return new_c;
}

// Local Variables:
// allout-layout: t
// js-indent-level: 2
Expand Down
13 changes: 7 additions & 6 deletions lib/palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ var color_convert = require('./convert');

/**
* Returns the hash key used for a {rgbcolor} in a {palettemap}
* @param {rgbcolor} c should have fields R,G,B
* @param {rgbcolor} c should have fields R,G,B (case insensitive)
* @return {string}
*/
function palette_map_key(c)
{
c = color_convert.normalize_rgb(c);
var s = "R" + c.R + "B" + c.B + "G" + c.G;
if ("A" in c) {
s = s + "A" + c.A;
Expand All @@ -71,10 +72,10 @@ function lab_palette_map_key(c)

/**
* Returns a mapping from each color in a to the closest/farthest color in b
* @param [{rgbcolor}] a each element should have fields R,G,B
* @param [{rgbcolor}] b each element should have fields R,G,B
* @param {rgbcolor} Optional background color when using alpha channels
* @param 'type' should be the string 'closest' or 'furthest'
* @param [{rgbcolor}] a each element should have fields R,G,B (case insensitive)
* @param [{rgbcolor}] b each element should have fields R,G,B (case insensitive)
* @param {('closest'|'furthest')} type should be the string 'closest' or 'furthest'
* @param {rgbcolor} bc Optional background color when using alpha channels
* @return {palettemap}
*/
function map_palette(a, b, type, bc)
Expand Down Expand Up @@ -143,7 +144,7 @@ function match_palette_lab(target_color, palette, find_furthest)
* Returns a mapping from each color in a to the closest color in b
* @param [{labcolor}] a each element should have fields L,a,b
* @param [{labcolor}] b each element should have fields L,a,b
* @param 'type' should be the string 'closest' or 'furthest'
* @param {('closest'|'furthest')} type should be the string 'closest' or 'furthest'
* @return {labpalettemap}
*/
function map_palette_lab(a, b, type)
Expand Down
77 changes: 50 additions & 27 deletions test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,66 @@ var color_convert = require('../lib/convert');
describe('convert', function(){
describe('#rgb_to_lab()', function(){
it('should convert to expected lab color #1', function(){
assert.deepEqual({'L' : 40.473, 'a' : -6.106, 'b' : -21.417},
round_all(color_convert.rgb_to_lab({'R' : 55,
'G' : 100,
'B' : 130})));
assert.deepStrictEqual({'L' : 40.473, 'a' : -6.106, 'b' : -21.417},
round_all(color_convert.rgb_to_lab({'R' : 55,
'G' : 100,
'B' : 130})));
});
it('should convert to expected lab color #2', function(){
assert.deepEqual({'L' : 0, 'a' : 0, 'b' : 0},
round_all(color_convert.rgb_to_lab({'R' : 0,
'G' : 0,
'B' : 0})));
assert.deepStrictEqual({'L' : 0, 'a' : 0, 'b' : 0},
round_all(color_convert.rgb_to_lab({'R' : 0,
'G' : 0,
'B' : 0})));
});
it('should convert to expected lab color #3', function(){
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgb_to_lab({'R' : 255,
'G' : 255,
'B' : 255})));
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgb_to_lab({'R' : 255,
'G' : 255,
'B' : 255})));
});
it('should convert to expected lab color #4', function(){
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgba_to_lab({'R' : 255,
'G' : 255,
'B' : 255,
'A' : 1.0})));
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgba_to_lab({'R' : 255,
'G' : 255,
'B' : 255,
'A' : 1.0})));
});
it('should convert to expected lab color #5', function(){
assert.deepEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgba_to_lab({'R' : 0,
'G' : 0,
'B' : 0,
'A' : 0.0})));
assert.deepStrictEqual({'L' : 100, 'a' : 0.005, 'b' : -0.010},
round_all(color_convert.rgba_to_lab({'R' : 0,
'G' : 0,
'B' : 0,
'A' : 0.0})));
});
it('should convert to expected lab color #6', function(){
assert.deepEqual({"L": 53.389, "a": 0.003, "b": -0.006},
round_all(color_convert.rgba_to_lab({'R' : 0,
'G' : 0,
'B' : 0,
'A' : 0.5})));
assert.deepStrictEqual({"L": 53.389, "a": 0.003, "b": -0.006},
round_all(color_convert.rgba_to_lab({'R' : 0,
'G' : 0,
'B' : 0,
'A' : 0.5})));
});
it('should convert to expected lab color #6 from lowercase RGBA object', function(){
assert.deepStrictEqual({"L": 53.389, "a": 0.003, "b": -0.006},
round_all(color_convert.rgba_to_lab({'r' : 0,
'g' : 0,
'b' : 0,
'a' : 0.5})));
});
})

describe('#normalize_rgb()', function () {
it('should convert lowercase RGB props to uppercase', function () {
assert.deepStrictEqual({'R': 55, 'G': 255, 'B': 0},
color_convert.normalize_rgb({'r': 55,
'g': 255,
'b': 0}));
});
it('should convert lowercase RGBA props to uppercase', function () {
assert.deepStrictEqual({'R': 55, 'G': 255, 'B': 0, 'A': 0},
color_convert.normalize_rgb({'r': 55,
'g': 255,
'b': 0,
'a': 0}));
});
})
});
Expand Down
Loading

0 comments on commit 6a65e9f

Please sign in to comment.