The testCIED2000
function calculates the CIEDE2000 color difference between two colors, "red" and "darkred", using the rgb2labRet
and CIEDE2000
functions, and logs and asserts the result. The function is designed to test the accuracy of the color difference calculation, and returns the calculated value.
npm run import -- "try ciede2000"
import assert from 'assert'
import colors from 'color-name'
async function testCIED2000() {
const {rgb2labRet} = await importer.import("rgb 2 lab")
const {CIEDE2000} = await importer.import("ciede2000")
let [R, G, B] = colors.red
let [R2, G2, B2] = colors.darkred
let [l, a, b] = rgb2labRet(3, R, G, B)
let [l2, a2, b2] = rgb2labRet(3, R2, G2, B2)
let ciede = CIEDE2000(l, a, b, l2, a2, b2)
console.log(ciede)
assert.ok(ciede < 20, 'colors are the same')
return ciede
}
export default testCIED2000
import { assert } from 'assert';
import colorName from 'color-name';
/**
* Calculates the CIEDE2000 color difference between two colors.
*
* @returns {Promise<number>} The calculated color difference.
*/
async function testCIED2000() {
// Import necessary functions
const { rgb2lab } = await importCIED2000Functions('rgb 2 lab');
const { CIEDE2000 } = await importCIED2000Functions('ciede2000');
// Define target colors
const targetColor = colorName.red;
const targetColorDark = colorName.darkred;
// Convert color names to RGB format
const [targetR, targetG, targetB] = targetColor;
const [targetR2, targetG2, targetB2] = targetColorDark;
// Convert RGB to CIELAB
const labValues = rgb2lab(3, targetR, targetG, targetB);
const labValuesDark = rgb2lab(3, targetR2, targetG2, targetB2);
// Calculate CIEDE2000 color difference
const ciede = CIEDE2000(labValues[0], labValues[1], labValues[2], labValuesDark[0], labValuesDark[1], labValuesDark[2]);
// Assert color difference is less than 20
assert.ok(ciede < 20, 'Colors are the same');
// Log and return calculated color difference
console.log(`CIEDE2000 color difference: ${ciede}`);
return ciede;
}
// Helper function to import required functions for CIEDE2000
async function importCIED2000Functions(moduleName) {
try {
const module = await import(moduleName);
return module;
} catch (error) {
throw new Error(`Failed to import ${moduleName} module: ${error.message}`);
}
}
export default testCIED2000;
import assert from 'assert'
import colors from 'color-name'
assert
function from the built-in Node.js module.colors
module from the color-name
package, which is likely a library for working with colors.async function testCIED2000() {
testCIED2000
.const {rgb2labRet} = await importer.import('rgb 2 lab')
const {CIEDE2000} = await importer.import('ciede2000')
rgb2labRet
function and CIEDE2000
function from other modules using the importer
object.let [R, G, B] = colors.red
let [R2, G2, B2] = colors.darkred
colors
object.let [l, a, b] = rgb2labRet(3, R, G, B)
let [l2, a2, b2] = rgb2labRet(3, R2, G2, B2)
rgb2labRet
function.rgb2labRet
is likely the number of bits per component (3 for 8-bit color).let ciede = CIEDE2000(l, a, b, l2, a2, b2)
CIEDE2000
function.console.log(ciede)
assert.ok(ciede < 20, 'colors are the same')
return ciede
export default testCIED2000
testCIED2000
function as the default export of the module.