Types
Color
Represents an RGB color.
The red channel value (0-255)
The green channel value (0-255)
The blue channel value (0-255)
The alpha channel value (0-1). Optional.
fromRgb
Creates an RGB color from the given RGB values.
The red channel value (0-255)
The green channel value (0-255)
The blue channel value (0-255)
The alpha channel value (0-1)
Example
import { fromRgb } from "@temelj/color";
const red = fromRgb(255, 0, 0);
console.log(red); // { red: 255, green: 0, blue: 0 }
const semiTransparentBlue = fromRgb(0, 0, 255, 0.5);
console.log(semiTransparentBlue);
// { red: 0, green: 0, blue: 255, alpha: 0.5 }
isValidRgb
Checks if a color is a valid RGB color.
True if the color is valid, false otherwise
Example
import { isValidRgb, fromRgb } from "@temelj/color";
const valid = fromRgb(100, 150, 200);
console.log(isValidRgb(valid)); // true
const invalid = { red: 300, green: 150, blue: 200 };
console.log(isValidRgb(invalid)); // false (red > 255)
parseRgb
Parses an RGB color string.
The RGB color string (e.g., “rgb(255, 0, 0)” or “rgba(255, 0, 0, 0.5)”)
The parsed color, or undefined if the string is invalid
Example
import { parseRgb } from "@temelj/color";
const color1 = parseRgb("rgb(255, 0, 0)");
console.log(color1); // { red: 255, green: 0, blue: 0 }
const color2 = parseRgb("rgba(100, 150, 200, 0.8)");
console.log(color2);
// { red: 100, green: 150, blue: 200, alpha: 0.8 }
const invalid = parseRgb("invalid");
console.log(invalid); // undefined
toRgbString
Converts a color to a CSS rgb color string.
Example
import { fromRgb, toRgbString } from "@temelj/color";
const color = fromRgb(255, 100, 50);
const str = toRgbString(color);
console.log(str); // "rgb(255, 100, 50)"
toRgbaString
Converts a color to a CSS rgba color string.
Example
import { fromRgb, toRgbaString } from "@temelj/color";
const color = fromRgb(255, 100, 50, 0.7);
const str = toRgbaString(color);
console.log(str); // "rgba(255, 100, 50, 0.7)"
const opaque = fromRgb(255, 100, 50);
const opaqueStr = toRgbaString(opaque);
console.log(opaqueStr); // "rgba(255, 100, 50, 1)"
lerpRgb
Interpolates between two RGB colors.
The interpolation factor (0-1)
Example
import { fromRgb, lerpRgb } from "@temelj/color";
const red = fromRgb(255, 0, 0);
const blue = fromRgb(0, 0, 255);
// Midpoint between red and blue
const purple = lerpRgb(red, blue, 0.5);
console.log(purple); // { red: 127.5, green: 0, blue: 127.5 }
// Closer to red
const reddishPurple = lerpRgb(red, blue, 0.25);
console.log(reddishPurple); // { red: 191.25, green: 0, blue: 63.75 }