Skip to content

Commit c9cc194

Browse files
committed
Update 2
1 parent a2c31c9 commit c9cc194

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

js/utils/__tests__/munsell.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const { interpColor, getMunsellColor, getcolor, searchColors } = require('../munsell');
2+
3+
global.createjs = {
4+
Graphics: {
5+
getRGB: (r, g, b, a) => {
6+
return `rgba(${r}, ${g}, ${b}, ${a})`;
7+
},
8+
},
9+
};
10+
11+
describe('munsell', () => {
12+
13+
describe('interpColor', () => {
14+
it('should interpolate between two colors', () => {
15+
expect(
16+
interpColor('#ff0000', '#0000ff', 0.5) // red and blue
17+
).toBe('rgba(127, 0, 127, 1)'); // purple
18+
expect(
19+
interpColor('#00ff00', '#000000', 0.75)
20+
).toBe('rgba(0, 191, 0, 1)');
21+
});
22+
23+
it('should return the first color if p = 1', () => {
24+
expect(
25+
interpColor('#123456', '#abcdef', 1)
26+
).toBe('#123456');
27+
});
28+
29+
it('should return the second color if p = 0', () => {
30+
expect(
31+
interpColor('#123456', '#abcdef', 0)
32+
).toBe('#abcdef');
33+
});
34+
35+
it('should handle undefined colors gracefully', () => {
36+
expect(
37+
interpColor(undefined, '#123456', 0.5)
38+
).toBe('rgba(18, 52, 86, 1)');
39+
expect(
40+
interpColor('#123456', undefined, 0.5)
41+
).toBe('rgba(18, 52, 86, 1)');
42+
});
43+
});
44+
45+
describe('getMunsellColor', () => {
46+
it('should return the correct Munsell color', () => {
47+
const color = getMunsellColor(50, 50, 50);
48+
expect(color).toMatch(/^#[0-9a-fA-F]{6}$/); // Ensure valid hex color format
49+
});
50+
51+
it('should handle edge cases for hue, value, and chroma', () => {
52+
expect(getMunsellColor(0, 0, 0)).toBeDefined();
53+
expect(getMunsellColor(100, 100, 100)).toBeDefined();
54+
expect(getMunsellColor(-10, -10, -10)).toBeDefined();
55+
expect(getMunsellColor(110, 110, 110)).toBeDefined();
56+
});
57+
});
58+
59+
describe('getcolor', () => {
60+
it('should return a valid array for a given color value', () => {
61+
const color = getcolor(50);
62+
expect(Array.isArray(color)).toBe(true);
63+
expect(color.length).toBe(3);
64+
expect(typeof color[0]).toBe('number');
65+
expect(typeof color[1]).toBe('number');
66+
expect(color[2]).toMatch(/^#[0-9a-fA-F]{6}$/); // Ensure RGB component is a valid hex color
67+
});
68+
69+
it('should handle edge cases for color value', () => {
70+
expect(getcolor(0)).toBeDefined();
71+
expect(getcolor(-10)).toBeDefined();
72+
expect(getcolor(110)).toBeDefined();
73+
});
74+
});
75+
76+
describe('searchColors', () => {
77+
it('should return a color value between 0 and 100', () => {
78+
const color = searchColors(128, 128, 128);
79+
expect(color).toBeGreaterThanOrEqual(0);
80+
expect(color).toBeLessThanOrEqual(100);
81+
});
82+
83+
it('should find the nearest color for black', () => {
84+
const color = searchColors(0, 0, 0);
85+
const nearestColor = getcolor(color);
86+
expect(nearestColor[2]).toMatch(/^#[0-9a-fA-F]{6}$/);
87+
});
88+
89+
it('should identify a close match for a RGB value', () => {
90+
const color = searchColors(100, 150, 200);
91+
const nearestColor = getcolor(color);
92+
expect(nearestColor[2]).toMatch(/^#[0-9a-fA-F]{6}$/);
93+
});
94+
});
95+
});

js/utils/munsell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6858,7 +6858,7 @@ let searchColors = (r, g, b) => {
68586858

68596859
return nearestColor;
68606860
};
6861-
6861+
module.exports = {interpColor, getMunsellColor, getcolor, searchColors};
68626862
// /**
68636863
// * @deprecated
68646864
// * @param {number} r - intensity of red

0 commit comments

Comments
 (0)