cssColorToHex turns a CSS color, such as "#ff8800", "rgba(255, 0, 0, 0.5)" or "tomato", into the 0xRRGGBBAA integer that Jimp uses for colors.
Use it to pass user-supplied or CSS colors to methods that only take numbers, such as setPixelColor.
Signature
import { cssColorToHex } from "jimp";
cssColorToHex(cssColor: string | number, colorSpace?: ColorSpace): numberDespite the name, it returns a number, not a hex string.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
cssColor |
string | number |
required | A CSS color string. A number is returned unchanged, so you can pass either kind. |
colorSpace |
ColorSpace |
SRGB |
The color space of the image the color is for. Pass DISPLAY_P3, exported from @jimp/utils, to get the value for a Display P3 image. See Color spaces. |
Example
import { Jimp, cssColorToHex } from "jimp";
cssColorToHex("#ff8800"); // 0xff8800ffcssColorToHex("#f80"); // 0xff8800ffcssColorToHex("#ff880080"); // 0xff880080cssColorToHex("rgba(255, 0, 0, 0.5)"); // 0xff000080cssColorToHex("hsl(120, 100%, 50%)"); // 0x00ff00ffcssColorToHex("red"); // 0xff0000ffcssColorToHex("transparent"); // 0x00000000cssColorToHex("color(srgb 1 0.5 0 / 0.5)"); // 0xff800080cssColorToHex(0x123456ff); // 0x123456ff, numbers pass through
const image = new Jimp({ width: 10, height: 10 });image.setPixelColor(cssColorToHex("tomato"), 0, 0);Supported formats
Parsing is done by TinyColor, so it accepts what TinyColor accepts:
- Hex:
#rgb,#rgba,#rrggbband#rrggbbaa, with or without the#. rgb()andrgba(), with numbers or percentages.hsl(),hsla(),hsv()andhsva().- The CSS color names, such as
"rebeccapurple", and"transparent". color(srgb r g b)andcolor(display-p3 r g b), with channels from0to1or percentages, and an optional/ alpha.
Alpha values from 0 to 1 become 0 to 255. Colors without alpha are fully opaque.
Color spaces
As in CSS, every color except color(display-p3 …) is sRGB.
By default cssColorToHex returns sRGB values, so a Display P3 color more saturated than sRGB can show is clipped:
cssColorToHex("color(display-p3 1 0 0)"); // 0xff0000ff, clipped to sRGB redPass a color space to get the value for an image in that space. On a Display P3 image, sRGB red is a less saturated value:
import { DISPLAY_P3 } from "@jimp/utils";
cssColorToHex("#ff0000", DISPLAY_P3); // 0xea3323ffcssColorToHex("color(display-p3 1 0 0)", DISPLAY_P3); // 0xff0000ffJimp’s own methods that take CSS colors, such as new Jimp({ color }), border and boxShadow, already convert them to the image’s space. See Color Spaces.
Notes
- Invalid colors throw. A string that isn’t a color throws
Invalid color "notacolor", and a bad channel incolor()throws, for example,Invalid color "color(display-p3 foo 0 0)": bad channel "foo". Wrap the call intry/catchif the color comes from user input. - Out-of-range channels are clamped.
"rgb(300, 0, 0)"becomes0xff0000ff. - Space-separated alpha is lost.
"rgb(0 0 255 / 50%)"returns an opaque color,0x0000ffff. Use"rgba(0, 0, 255, 0.5)"for a semi-transparent color. - Where you don’t need it.
new Jimp({ color }),border,boxShadowand themixandxoractions ofcoloraccept CSS strings and convert them the same way, sonew Jimp({ width: 10, height: 10, color: "tomato" })works without callingcssColorToHex. - Added in 2.0: the
colorSpaceparameter, andcolor(srgb …)andcolor(display-p3 …)colors. - Changed in 2.0: invalid colors throw. Before, they became opaque black,
0x000000ff. - Changes from v0.
cssColorToHexused to be a static method,Jimp.cssColorToHex. It’s now a named export ofjimp(and of@jimp/utils).