modules/colour/colour library

The Collect Colour system — a multi-format colour class that goes beyond Flutter's Color.

This library provides Colour, a drop-in replacement for Color that adds support for multiple colour spaces (RGB, HSL, HSV), numerous input formats (hex, percentage, fraction, base-256), and seamless conversion between them.

Colour Spaces

  • Colour — The primary ARGB colour class (implements Color).
  • HSVColour — Hue, Saturation, Value representation (implements HSVColor).
  • HSLColour — Hue, Saturation, Lightness representation (implements HSLColor).
  • ColourSpace — The sealed base class that defines the conversion contract.

Quick Start

// From hex
final red = Colour.fromHex(hexString: '#FF0000');

// From RGB components
final blue = Colour.fromRGB(red: 0, green: 0, blue: 255);

// From HSV values
final green = Colour.fromHSV(h: 120, s: 1.0, v: 1.0);

// Convert between spaces
final hsv = red.toHSV();
final hsl = red.toHSL();

// Output in different formats
print(red.hex);   // 'FFFF0000'
print(red.rgb);   // '255,0,0'
print(red.b256);  // base-256 encoded

Classes

Colour
A rich colour class that implements Flutter's Color interface while adding multi-format input/output and colour space conversions.
ColourSpace
The sealed base class for all colour space representations in Collect.
HSLColour
A colour represented in the HSL (Hue, Saturation, Lightness) colour space.
HSVColour
A colour represented in the HSV (Hue, Saturation, Value) colour space.

Extensions

c on Color
Convenience extension to convert any Flutter Color to a Colour.
Hslcolour on HSLColor
Convenience extension to convert a Flutter HSLColor to an HSLColour.
Hsvcolour on HSVColor
Convenience extension to convert a Flutter HSVColor to an HSVColour.

Constants

kCompleteValidHexPattern → const String
Regex pattern that matches complete, valid hex colour strings (3, 6, or 8 hex chars, optional leading #).
kValidHexPattern → const String
Regex pattern that matches partial hex colour strings (1-8 hex chars, optional leading #).

Functions

colorFromHex(String inputString, {bool enableAlpha = true}) Color?
Parses a hex colour string into a Flutter Color, or returns null if the string is invalid.
colorToHex(Color color, {bool includeHashSign = false, bool enableAlpha = true, bool toUpperCase = true}) String
Converts a Flutter Color to a hex string.
hslToHsv(HSLColor color) HSVColour
Converts an HSLColor to an HSVColour.
hsvToHsl(HSVColor color) HSLColour
Converts an HSVColor to an HSLColour.
useWhiteForeground(Color backgroundColor, {double bias = 0.0}) bool
Returns true if white text should be used on the given backgroundColor for readability.