color_convert_null_safety 1.0.2

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A color conversion library for Dart. It converts all ways between rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex strings, and CSS keywords (will round to closest) and supporting null safety.

color_convert

pub package

A color conversion library for Dart. It converts all ways between rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex strings, and CSS keywords (will round to closest). Based on color-convert by Heather Arthur.

Usage

import 'package:color_convert_null_safety/color_convert.dart';

void main() {
  convert.rgb.hsl(140, 200, 100); // [96, 48, 59]
  convert.keyword.rgb('blue'); // [0, 0, 255]

  var rgbChannels = convert.rgb.channels; // 3
  var cmykChannels = convert.cmyk.channels; // 4
  var ansiChannels = convert.ansi16.channels; // 1
}

API

Simply get the property of the from and to conversion that you're looking for.

All functions have a rounded and unrounded variant. By default, return values are rounded. To get the unrounded (raw) results, simply tack on .raw to the function.

All 'from' functions have a hidden property called .channels that indicates the number of channels the function expects (not including alpha).

import 'package:color_convert/color_convert.dart';

void main() {
  // Hex to LAB
  convert.hex.lab('DEADBF');         // [ 76, 21, -2 ]
  convert.hex.lab.raw('DEADBF');     // [ 75.56213190997677, 20.653827952644754, -2.290532499330533 ]

  // RGB to CMYK
  convert.rgb.cmyk(167, 255, 4);     // [ 35, 0, 98, 0 ]
  convert.rgb.cmyk.raw(167, 255, 4); // [ 34.509803921568626, 0, 98.43137254901961, 0 ]
}

Arrays

All functions that accept multiple arguments also support passing an array.

Note that this does not apply to functions that convert from a color that only requires one value (e.g. keyword, ansi256, hex, etc.)

import 'package:color_convert/color_convert.dart';

void main() {
  convert.rgb.hex(123, 45, 67);      // '7B2D43'
  convert.rgb.hex([123, 45, 67]);    // '7B2D43'
}

Color Space Scales

Conversions rely on an agreed upon 'full-scale' value for each of the channels. Listed here are those values for the most common color spaces

rgb

channelfull-scale value
r255
g255
b255

hsl

channelfull-scale value
h360
s100
l100

hsv

channelfull-scale value
h360
s100
v100

hwb

channelfull-scale value
h360
w100
b100

cmyk

channelfull-scale value
c100
m100
y100
k100

hex

channelfull-scale value
hex0xffffff

keyword

channelvalue
nameany key from color-name

apple

channelfull-scale value
065535
165535
265535

gray

channelfull-scale value
g100

License

MIT License.