1.2.6 • Published 6 months ago

color-translate v1.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Color Translator

This package allows translating colors among all valid css formats

🌐 Links

Website

Visual Studio Code Extension

NPM

Github Repository

Installation

Using npm:

npm i color-translate

Formats supported

All formats except "Named" accept an alpha value. (See Formats specification)

FormatExample stringExample obj
HEX'#FF6600'-
HEX 0x'0xFF6600'-
RGB'rgb(255 100 0)'{ r: 255, g: 100, b: 0 }
HSL'hsl(18 100% 50%)'{ h: 18, s: '100%', l: '50%' }
HWB'hwb(18 6% 0.4%)'{ h: 18, w: '6%', w: '0.4%' }
LAB'lab(61.6 61.3 66)'{ l: 61.6, a: 61.3, b: 66 }
LCH'lch(60 91.5 44)'{ l: 60, c: 91.5, h: 44 }
OKLAB'oklab(0.7 0.13 0.14)'{ l: 0.7, a: 0.13, b: 0.14, ok: true }
OKLCH'oklch(0.7 0.2 48)'{ l: 0.7, c: 0.2, h: 48, ok: true }
CMYK'device-cmyk(0 0.5 1 0)'{ c: 0, m: 0.5, y: 1, k: 0 }
A98'color(a98-rgb 1 0.4 0)'{ r: 1, g: 0.4, b: 0, a98: true }
Named'orangered'-

Usage

The first step is to create a new ColorTranslator instance

import ColorTranslator from "color-translate";
const color = new ColorTranslator(<input>);

The input value has to be either a valid string or an object (See Formats supported)

const color = new ColorTranslator("rgb(255 100 0)");
const color2 = new ColorTranslator({ r: 255, g: 100, b: 0 });

Example

import ColorTranslator from "color-translate";

const color = new ColorTranslator("hsl(0 100% 50%)");
color
  .updateRgb({ r: 127.5, b: 255, alpha: 0.4 })
  .updateOptions({ angleUnit: "grad" });

console.log(color.lch.l);
// 39.282796563892475

console.log(color.lch.toString());
// 'lch(39.28 121.27 342.22grad / 0.4)'

console.log(color.lch.toString({ angleUnit: "rad" }));
// 'lch(39.28 121.27 5.38rad / 0.4)'

Methods

Once created the ColorTranslator instance, we can call these methods

MethodDescriptionExample
.\<format>Returns a color object, containing it's values as properties.rgb = { r: 255, g: 100, b: 0 }
.\<format>.toString()Returns a string representation of the color in the given format.rgb.toString() = rgb(255, 100, 0)
.update\<format>(...)Updates specific properties in the given format.updateRgb({ r: 0 }).rgb = { r: 0, g: 100, b: 0 }
.optionsReturns the current options. (See Options).options = { spaced: true, ... }
.updateOptions(...)Updates the given options. (See Options).updateOptions({ spaced: false }).options = { spaced: false, ... }

Options

OptionDescriptionDefault value
angleUnitAngle unit'none'
cacheInputPersist last inputfalse
legacyLegacy modetrue
limitToColorSpaceClamp colors to format spacetrue
maxDigitsRound color values to a max number of decimal places2
spacedSpace between string color valuesfalse

Formats specification

Here is a table with the standard presentations of each color format property (this follows closely the W3C standards)

Color Format/sProperty/iesPresentationsExamples
ALL FORMATSalpha (default = 1)Number/String 0-1, String Percentage0.5, '0.5', '50%'
RGBr, g, bNumber/String 0-255, String Percentage123, '123', '50%'
A98r, g, bNumber/String 0-10.5, '0.5'
HSL, HWB, LCH, OKLCHhNumber/String 0-360, String Percentage, String Grad, String Rad, String Turn180, '180', '50%', '200grad', '3.14rad', '0.5turn'
HSLs, lNumber/String 0-1, String Percentage0.5, '0.5', '50%'
HWBw, bNumber/String 0-1, String Percentage0.5, '0.5', '50%'
LAB, LCHlNumber/String 0-100, String Percentage50, '50', '50%'
LABa, bNumber/String -125 - 125, String Percentage -100% - 100%-62.5, '-62.5', '-50%'
LCHcNumber/String 0-150, String Percentage75, '75', '50%'
OKLAB, OKLCHlNumber/String 0-1, String Percentage0.5, '0.5', '50%'
OKLABa, bNumber/String -0.4 - 0.4, String Percentage -100% - 100%-0.2, '-0.2', '-50%'
OKLCHcNumber/String 0-0.4, String Percentage0.2, '0.2', '50%'
CMYKc, m, y, kNumber/String 0-1, String Percentage0.5, '0.5', '50%'
1.2.6

6 months ago

1.2.3

6 months ago

1.2.0

12 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

0.5.2

1 year ago

0.5.0

2 years ago

0.5.1

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.4.2

2 years ago

0.3.0

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.1.3

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

1.0.0

6 years ago