Name That Color JS (NTCJS)

raw JSON →
1.1.3 verified Thu Apr 23 auth: no javascript maintenance

NTCJS is a Node.js CommonJS-compatible wrapper for the original Name That Color JavaScript library by Chirag Mehta. It provides a straightforward utility to convert hexadecimal color codes (e.g., `#6195ED`) into human-readable color names, along with the closest matching RGB value and an exact match boolean. The current stable version, 1.1.3 (or 1.1.4 for `@trihargianto/ntcjs`), has not been updated in several years, indicating a maintenance or effectively abandoned status. It primarily targets CommonJS environments, but ships TypeScript types for enhanced developer experience. Its key differentiator is its simplicity and direct port of the well-known 'Name That Color' algorithm, without additional features like i18n or advanced color manipulation found in other libraries.

error TypeError: ntc is not a function
cause Attempting to call the imported `ntc` module directly as a function instead of using its `name` method.
fix
Call the name method: ntc.name('#6195ED');
error ReferenceError: require is not defined
cause Attempting to use `require()` in a Node.js ESM module context without proper configuration or transpilation.
fix
In a Node.js ESM project, use import ntc from 'ntcjs'; if your setup (e.g., bundler or package.json exports field) allows interoperability, or consider dynamic import: const ntc = await import('ntcjs');.
error Property 'name' does not exist on type 'string[]'.
cause Trying to access `n_match.name` on the array returned by `ntc.name()`, which is a common JavaScript mistake when an array is mistaken for an object with named properties.
fix
Access array elements by index: n_match[1] for the name. For better readability, destructure the array: const [rgb, name, exact] = ntc.name('#HEX');.
gotcha The `ntcjs` package is primarily a CommonJS module. While it ships TypeScript types, native Node.js ESM environments might require specific `package.json` configurations (`"type": "module"` with `"exports"`) or dynamic `import()` to use it, or a bundler to transpile correctly.
fix For Node.js ESM, consider using `import(pkgName)` for dynamic imports. For TypeScript, ensure your `tsconfig.json` `module` option is compatible (e.g., `"CommonJS"` or a bundler-friendly option) or configure your build system to handle CJS imports in ESM contexts. If you encounter `ERR_REQUIRE_ESM`, you'll need to adjust your module resolution strategy.
gotcha The `ntc.name()` function returns an array, not an object. Developers often forget the order of elements (RGB value, color name, exact match boolean) and might access `n_match.name` instead of `n_match[1]`.
fix Destructure the array immediately after calling `ntc.name()` for better readability and maintainability: `const [rgbValue, colorName, isExactMatch] = ntc.name('#HEX');`.
gotcha The library expects hexadecimal color strings as input, typically prefixed with '#'. Providing invalid formats (e.g., shorthand hex, named colors, RGB/HSL strings, or hex without '#') may lead to incorrect results or errors. The original NTC library primarily deals with hex input.
fix Always ensure the input is a 6-digit hexadecimal string prefixed with '#', such as '#RRGGBB'. Validate user input if colors are dynamic.
deprecated The `ntcjs` package has not received updates for several years (version 1.1.3 published 5 years ago, although `@trihargianto/ntcjs` is 1.1.4 and 2 years old). While the core functionality is stable, it means no new features, performance improvements, or official security patches are likely to be released.
fix For new projects requiring active maintenance or broader color space support (like HSL, CMYK, i18n, or more modern color names), consider alternatives like `chroma.js` or wrappers that build upon it, such as `@oz-tal/name-that-color-and-hue-i18n`, which actively maintain and extend similar functionality.
npm install ntcjs
yarn add ntcjs
pnpm add ntcjs

This quickstart demonstrates how to import and use the `ntcjs` library to get the name of a given hexadecimal color code. It shows the structured array output including the closest RGB value, the color name, and whether it was an exact match. It uses `import` syntax suitable for TypeScript.

import { readFileSync } from 'node:fs';
import ntc from 'ntcjs'; // In a real ESM setup, this might require a bundler or Node.js --experimental-json-modules

// Example of usage from the README
const hexColor = '#6195ED';
const n_match = ntc.name(hexColor);

const n_rgb = n_match[0]; // RGB value of closest match
const n_name = n_match[1]; // Text string: Color name
const n_exactmatch = n_match[2]; // True if exact color match

console.log(`Input Hex: ${hexColor}`);
console.log(`Closest RGB: ${n_rgb}`);
console.log(`Color Name: ${n_name}`);
console.log(`Exact Match: ${n_exactmatch}`);

// Demonstrating with another color
const anotherHex = '#FF0000';
const another_match = ntc.name(anotherHex);
console.log(`\nInput Hex: ${anotherHex}`);
console.log(`Closest RGB: ${another_match[0]}`);
console.log(`Color Name: ${another_match[1]}`);
console.log(`Exact Match: ${another_match[2]}`);

// Demonstrating with a slightly off color
const offHex = '#FE0001';
const off_match = ntc.name(offHex);
console.log(`\nInput Hex: ${offHex}`);
console.log(`Closest RGB: ${off_match[0]}`);
console.log(`Color Name: ${off_match[1]}`);
console.log(`Exact Match: ${off_match[2]}`);