Skip to content

Commit

Permalink
Add rgbToHex, use it in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Mar 2, 2022
1 parent f7b649b commit 43dd765
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
11 changes: 11 additions & 0 deletions src/utils/colour.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ dwv.utils.hexToRgb = function (hexStr) {
};
};

/**
* Convert RGB to its hex equivalent.
*
* @param {object} rgb The RGB object as {r,g,b}.
* @returns {string} A string representing the hex color as '#ab01ef'.
*/
dwv.utils.rgbToHex = function (rgb) {
return '#' +
((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);
};

/**
* Get the brightness of a RGB colour: calculates
* the luma (Y) of the YIQ colour space.
Expand Down
46 changes: 27 additions & 19 deletions tests/utils/colour.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,33 @@ QUnit.test('Test ybrToRgb.', function (assert) {
* @function module:tests/utils~hexToRgb
*/
QUnit.test('Test hexToRgb.', function (assert) {
var rgb00 = dwv.utils.hexToRgb('#000000');
assert.equal(rgb00.r, 0, 'hexToRgb #000000: r');
assert.equal(rgb00.g, 0, 'hexToRgb #000000: g');
assert.equal(rgb00.b, 0, 'hexToRgb #000000: b');

var rgb01 = dwv.utils.hexToRgb('#ffffff');
assert.equal(rgb01.r, 255, 'hexToRgb #ffffff: r');
assert.equal(rgb01.g, 255, 'hexToRgb #ffffff: g');
assert.equal(rgb01.b, 255, 'hexToRgb #ffffff: b');

var rgb02 = dwv.utils.hexToRgb('#7f7f7f');
assert.equal(rgb02.r, 127, 'hexToRgb #7f7f7f: r');
assert.equal(rgb02.g, 127, 'hexToRgb #7f7f7f: g');
assert.equal(rgb02.b, 127, 'hexToRgb #7f7f7f: b');

var rgb03 = dwv.utils.hexToRgb('#4e33d6');
assert.equal(rgb03.r, 78, 'hexToRgb #4e33d6: r');
assert.equal(rgb03.g, 51, 'hexToRgb #4e33d6: g');
assert.equal(rgb03.b, 214, 'hexToRgb #4e33d6: b');
var hex00 = '#000000';
var rgb00 = dwv.utils.hexToRgb(hex00);
assert.equal(rgb00.r, 0, 'hexToRgb #00: r');
assert.equal(rgb00.g, 0, 'hexToRgb #00: g');
assert.equal(rgb00.b, 0, 'hexToRgb #00: b');
assert.equal(dwv.utils.rgbToHex(rgb00), hex00, 'rgbToHex #00');

var hex01 = '#ffffff';
var rgb01 = dwv.utils.hexToRgb(hex01);
assert.equal(rgb01.r, 255, 'hexToRgb #01: r');
assert.equal(rgb01.g, 255, 'hexToRgb #01: g');
assert.equal(rgb01.b, 255, 'hexToRgb #01: b');
assert.equal(dwv.utils.rgbToHex(rgb01), hex01, 'rgbToHex #01');

var hex02 = '#7f7f7f';
var rgb02 = dwv.utils.hexToRgb(hex02);
assert.equal(rgb02.r, 127, 'hexToRgb #02: r');
assert.equal(rgb02.g, 127, 'hexToRgb #02: g');
assert.equal(rgb02.b, 127, 'hexToRgb #02: b');
assert.equal(dwv.utils.rgbToHex(rgb02), hex02, 'rgbToHex #02');

var hex03 = '#4e33d6';
var rgb03 = dwv.utils.hexToRgb(hex03);
assert.equal(rgb03.r, 78, 'hexToRgb #03: r');
assert.equal(rgb03.g, 51, 'hexToRgb #03: g');
assert.equal(rgb03.b, 214, 'hexToRgb #03: b');
assert.equal(dwv.utils.rgbToHex(rgb03), hex03, 'rgbToHex #03');
});

/**
Expand Down

0 comments on commit 43dd765

Please sign in to comment.