Standard Toolkit
Packages@accelint/converters

Hex Converters

Convert between hex color strings and RGBA 0-255 tuples

hexToRgba255Tuple

Parse a hex color string to a Rgba255Tuple.

Usage

import { hexToRgba255Tuple } from '@accelint/converters/hex';

// 6-character hex
const color = hexToRgba255Tuple('#FF8040');
// [255, 128, 64, 255]

// 3-character shorthand
const shortColor = hexToRgba255Tuple('#F84');
// [255, 136, 68, 255]

Reference

function hexToRgba255Tuple(hex: string): Rgba255Tuple

Parameters

ParameterTypeDescription
hexstringThe hex string to parse. Supports "#RGB", "#RGBA", "#RRGGBB", "#RRGGBBAA". Hash is optional.

Returns

Returns a Rgba255Tuple [r, g, b, a] where all values are 0-255.

Throws

Throws Error if the hex string is invalid.

Examples

Example: Standard 6-character hex

import { hexToRgba255Tuple } from '@accelint/converters/hex';

const color = hexToRgba255Tuple('#FF8040');
console.log(color);
// [255, 128, 64, 255]

Example: 3-character shorthand

import { hexToRgba255Tuple } from '@accelint/converters/hex';

// Each digit doubles: #F84 → #FF8844
const color = hexToRgba255Tuple('#F84');
console.log(color);
// [255, 136, 68, 255]

Example: With alpha channel

import { hexToRgba255Tuple } from '@accelint/converters/hex';

// 8-character hex includes alpha
const color = hexToRgba255Tuple('#FF804080');
console.log(color);
// [255, 128, 64, 128]

// 4-character shorthand with alpha
const shortColor = hexToRgba255Tuple('#F840');
console.log(shortColor);
// [255, 136, 68, 0]

Example: Without hash prefix

import { hexToRgba255Tuple } from '@accelint/converters/hex';

// Hash is optional
const color = hexToRgba255Tuple('FF8040');
console.log(color);
// [255, 128, 64, 255]

Good to know: 3-character hex values are expanded by doubling each digit (#RGB → #RRGGBB). 4-character hex values include alpha (#RGBA → #RRGGBBAA). If no alpha is specified, it defaults to 255 (fully opaque).

rgba255TupleToHex

Convert a Rgba255Tuple to a hex string.

Usage

import { rgba255TupleToHex } from '@accelint/converters/hex';

// Convert to hex without alpha
const hex = rgba255TupleToHex([255, 128, 64, 255]);
// "#FF8040"

// Include alpha channel
const hexWithAlpha = rgba255TupleToHex([255, 128, 64, 128], true);
// "#FF804080"

Reference

function rgba255TupleToHex(
  color: Rgba255Tuple,
  includeAlpha?: boolean
): string

Parameters

ParameterTypeDefaultDescription
colorRgba255Tuple-The Rgba255Tuple [r, g, b, a] where all values are 0-255.
includeAlphabooleanfalseWhether to include alpha channel in output.

Returns

Returns a hex string (e.g., "#FF8040" or "#FF804080").

Examples

Example: Basic conversion

import { rgba255TupleToHex } from '@accelint/converters/hex';

const hex = rgba255TupleToHex([255, 128, 64, 255]);
console.log(hex);
// "#FF8040"

Example: Including alpha channel

import { rgba255TupleToHex } from '@accelint/converters/hex';

const hexWithAlpha = rgba255TupleToHex([255, 128, 64, 128], true);
console.log(hexWithAlpha);
// "#FF804080"

Example: Round-trip conversion

import { hexToRgba255Tuple, rgba255TupleToHex } from '@accelint/converters/hex';

const original = '#FF8040';
const tuple = hexToRgba255Tuple(original);
const converted = rgba255TupleToHex(tuple);
console.log(converted === original);
// true

Good to know: By default, the alpha channel is omitted from the output. Set includeAlpha: true to include it. All hex values are uppercase.

On this page