Name That Color JS (NTCJS)
raw JSON →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.
Common errors
error TypeError: ntc is not a function ↓
name method: ntc.name('#6195ED'); error ReferenceError: require is not defined ↓
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[]'. ↓
n_match[1] for the name. For better readability, destructure the array: const [rgb, name, exact] = ntc.name('#HEX');. Warnings
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. ↓
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]`. ↓
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. ↓
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. ↓
Install
npm install ntcjs yarn add ntcjs pnpm add ntcjs Imports
- ntc wrong
import ntc from 'ntcjs';correctconst ntc = require('ntcjs'); - name wrong
ntc('#6195ED');correctntc.name('#6195ED'); - NTCResult wrong
import { NTCResult } from 'ntcjs';correctimport type { NTCResult } from 'ntcjs';
Quickstart
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]}`);