cied2000 | rgb 2 lab | | Search

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.

Run example

npm run import -- "try ciede2000"

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

What the code could have been:

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;

Code Breakdown

Import Statements

import assert from 'assert'
import colors from 'color-name'

Test Function

async function testCIED2000() {

Importing Modules

const {rgb2labRet} = await importer.import('rgb 2 lab')
const {CIEDE2000} = await importer.import('ciede2000')

Color Values

let [R, G, B] = colors.red
let [R2, G2, B2] = colors.darkred

Convert RGB to LAB Colors

let [l, a, b] = rgb2labRet(3, R, G, B)
let [l2, a2, b2] = rgb2labRet(3, R2, G2, B2)

Calculate CIEDE2000

let ciede = CIEDE2000(l, a, b, l2, a2, b2)

Logging and Assertion

console.log(ciede)
assert.ok(ciede < 20, 'colors are the same')
return ciede

Export Statement

export default testCIED2000