mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-05-30 15:14:35 -04:00
Add unit tests for new color-related functionality
Also, fix a math error in `Color.rgbToHsv`. Newly covered functions: - `toRbgColorObject` from `util/cast.js` - `hsvToRgb` from `util/colors.js` - `rgbToHsv` from `util/colors.js` - `mixRgb` from `util/colors.js`
This commit is contained in:
parent
a6190da774
commit
d0845728ee
3 changed files with 100 additions and 11 deletions
test/unit
|
@ -1,6 +1,43 @@
|
|||
var test = require('tap').test;
|
||||
var tap = require('tap');
|
||||
var test = tap.test;
|
||||
var color = require('../../src/util/color');
|
||||
|
||||
/**
|
||||
* Assert that two HSV colors are similar to each other, within a tolerance.
|
||||
* @param {Test} t - the Tap test object.
|
||||
* @param {HSVObject} actual - the first HSV color to compare.
|
||||
* @param {HSVObject} expected - the other HSV color to compare.
|
||||
*/
|
||||
var hsvSimilar = function (t, actual, expected) {
|
||||
if ((Math.abs(actual.h - expected.h) >= 1) ||
|
||||
(Math.abs(actual.s - expected.s) >= 0.01) ||
|
||||
(Math.abs(actual.v - expected.v) >= 0.01)
|
||||
) {
|
||||
t.fail('HSV colors not similar enough', {
|
||||
actual: actual,
|
||||
expected: expected
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Assert that two RGB colors are similar to each other, within a tolerance.
|
||||
* @param {Test} t - the Tap test object.
|
||||
* @param {RGBObject} actual - the first RGB color to compare.
|
||||
* @param {RGBObject} expected - the other RGB color to compare.
|
||||
*/
|
||||
var rgbSimilar = function (t, actual, expected) {
|
||||
if ((Math.abs(actual.r - expected.r) >= 1) ||
|
||||
(Math.abs(actual.g - expected.g) >= 1) ||
|
||||
(Math.abs(actual.b - expected.b) >= 1)
|
||||
) {
|
||||
t.fail('RGB colors not similar enough', {
|
||||
actual: actual,
|
||||
expected: expected
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
test('decimalToHex', function (t) {
|
||||
t.strictEqual(color.decimalToHex(0), '#000000');
|
||||
t.strictEqual(color.decimalToHex(1), '#000001');
|
||||
|
@ -60,3 +97,37 @@ test('hexToDecimal', function (t) {
|
|||
t.strictEqual(color.hexToDecimal('#00ffaa'), 65450);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('hsvToRgb', function (t) {
|
||||
rgbSimilar(t, color.hsvToRgb({h: 0, s: 0, v: 0}), {r: 0, g: 0, b: 0});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 123, s: 0.1234, v: 0}), {r: 0, g: 0, b: 0});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 0, s: 0, v: 1}), {r: 255, g: 255, b: 255});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 321, s: 0, v: 1}), {r: 255, g: 255, b: 255});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 0, s: 1, v: 1}), {r: 255, g: 0, b: 0});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 120, s: 1, v: 1}), {r: 0, g: 255, b: 0});
|
||||
rgbSimilar(t, color.hsvToRgb({h: 240, s: 1, v: 1}), {r: 0, g: 0, b: 255});
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('rgbToHsv', function (t) {
|
||||
hsvSimilar(t, color.rgbToHsv({r: 0, g: 0, b: 0}), {h: 0, s: 0, v: 0});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 64, g: 64, b: 64}), {h: 0, s: 0, v: 0.25});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 128, g: 128, b: 128}), {h: 0, s: 0, v: 0.5});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 192, g: 192, b: 192}), {h: 0, s: 0, v: 0.75});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 255, g: 255, b: 255}), {h: 0, s: 0, v: 1});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 255, g: 0, b: 0}), {h: 0, s: 1, v: 1});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 0, g: 255, b: 0}), {h: 120, s: 1, v: 1});
|
||||
hsvSimilar(t, color.rgbToHsv({r: 0, g: 0, b: 255}), {h: 240, s: 1, v: 1});
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('mixRgb', function (t) {
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, -1), {r: 10, g: 20, b: 30});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 0), {r: 10, g: 20, b: 30});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 0.25), {r: 15, g: 25, b: 35});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 0.5), {r: 20, g: 30, b: 40});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 0.75), {r: 25, g: 35, b: 45});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 1), {r: 30, g: 40, b: 50});
|
||||
rgbSimilar(t, color.mixRgb({r: 10, g: 20, b: 30}, {r: 30, g: 40, b: 50}, 2), {r: 30, g: 40, b: 50});
|
||||
t.end();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue