Skip to content

Commit

Permalink
Update 2 (#4132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Commanderk3 authored Dec 12, 2024
1 parent a2c31c9 commit 86872af
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
95 changes: 95 additions & 0 deletions js/utils/__tests__/munsell.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const { interpColor, getMunsellColor, getcolor, searchColors } = require('../munsell');

global.createjs = {
Graphics: {
getRGB: (r, g, b, a) => {
return `rgba(${r}, ${g}, ${b}, ${a})`;
},
},
};

describe('munsell', () => {

describe('interpColor', () => {
it('should interpolate between two colors', () => {
expect(
interpColor('#ff0000', '#0000ff', 0.5) // red and blue
).toBe('rgba(127, 0, 127, 1)'); // purple
expect(
interpColor('#00ff00', '#000000', 0.75)
).toBe('rgba(0, 191, 0, 1)');
});

it('should return the first color if p = 1', () => {
expect(
interpColor('#123456', '#abcdef', 1)
).toBe('#123456');
});

it('should return the second color if p = 0', () => {
expect(
interpColor('#123456', '#abcdef', 0)
).toBe('#abcdef');
});

it('should handle undefined colors gracefully', () => {
expect(
interpColor(undefined, '#123456', 0.5)
).toBe('rgba(18, 52, 86, 1)');
expect(
interpColor('#123456', undefined, 0.5)
).toBe('rgba(18, 52, 86, 1)');
});
});

describe('getMunsellColor', () => {
it('should return the correct Munsell color', () => {
const color = getMunsellColor(50, 50, 50);
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/); // Ensure valid hex color format
});

it('should handle edge cases for hue, value, and chroma', () => {
expect(getMunsellColor(0, 0, 0)).toBeDefined();
expect(getMunsellColor(100, 100, 100)).toBeDefined();
expect(getMunsellColor(-10, -10, -10)).toBeDefined();
expect(getMunsellColor(110, 110, 110)).toBeDefined();
});
});

describe('getcolor', () => {
it('should return a valid array for a given color value', () => {
const color = getcolor(50);
expect(Array.isArray(color)).toBe(true);
expect(color.length).toBe(3);
expect(typeof color[0]).toBe('number');
expect(typeof color[1]).toBe('number');
expect(color[2]).toMatch(/^#[0-9a-fA-F]{6}$/); // Ensure RGB component is a valid hex color
});

it('should handle edge cases for color value', () => {
expect(getcolor(0)).toBeDefined();
expect(getcolor(-10)).toBeDefined();
expect(getcolor(110)).toBeDefined();
});
});

describe('searchColors', () => {
it('should return a color value between 0 and 100', () => {
const color = searchColors(128, 128, 128);
expect(color).toBeGreaterThanOrEqual(0);
expect(color).toBeLessThanOrEqual(100);
});

it('should find the nearest color for black', () => {
const color = searchColors(0, 0, 0);
const nearestColor = getcolor(color);
expect(nearestColor[2]).toMatch(/^#[0-9a-fA-F]{6}$/);
});

it('should identify a close match for a RGB value', () => {
const color = searchColors(100, 150, 200);
const nearestColor = getcolor(color);
expect(nearestColor[2]).toMatch(/^#[0-9a-fA-F]{6}$/);
});
});
});
2 changes: 1 addition & 1 deletion js/utils/munsell.js
Original file line number Diff line number Diff line change
Expand Up @@ -6858,7 +6858,7 @@ let searchColors = (r, g, b) => {

return nearestColor;
};

module.exports = {interpColor, getMunsellColor, getcolor, searchColors};
// /**
// * @deprecated
// * @param {number} r - intensity of red
Expand Down

0 comments on commit 86872af

Please sign in to comment.