color 2.1.1

Color #

A simple Dart package exposing a Color class which can be used to create, convert, and compare colors.

Installation #

  1. Add this to your package's pubspec.yaml file:
  color: any
  1. Get the package using your IDE's GUI or via command line with
$ pub get
  1. Import the color.dart file in your app
import 'package:color/color.dart';

Usage #

Color objects can be constructed using any of a few available constructors.

To create a color from rgb values, call

Color rgbColor = new Color.rgb(192, 255, 238);
RgbColor rgbColor = new RgbColor(192, 255, 238);

Alternatively, a color can be created directly in a number of other color spaces.

RgbColor rgb = new RgbColor(192, 255, 238);
HexColor hex = new HexColor('c0ffee');
HslColor hsl = new HslColor(163.8, 100, 87.6);
XyzColor xyz = new XyzColor(72.931, 88.9, 94.204);
CielabColor cielab = new CielabColor(95.538, -23.02, 1.732);

Colors are immutable, and can be created using const constructors.

RgbColor rgb = const RgbColor(192, 255, 238);

Colors can be created directly from CSS3 color names.

RgbColor black = new'black'); //factory constructor that returns a const RgbColor
RgbColor white = RgbColor.namedColors['black']; //directly reference the const RgbColor without the factory

Colors implementing the CssColorSpace interface can output a css string representation.

RgbColor rgb = new RgbColor(192, 255, 238);
HexColor hex = new HexColor('c0ffee');
HslColor hsl = new HslColor(163.8, 100, 87.6);
assert(rgb is CssColorSpace, true);
assert(hex is CssColorSpace, true);
assert(hsl is CssColorSpace, true);
print(rgb.toCssString()); //rgb(192, 255, 238)
print(hex.toCssString()); //#c0ffee
print(hsl.toCssString()); //hsl(163.8, 100.0%, 87.6%)

Colors can be compared using the == operator, which will evaluate to true if the two colors share identical rgb values after being rounded to integers.

assert(new Color.hex('c0ffee') == new Color.hex('c0ffee'));
assert(new Color.rgb('192, 255, 238') == new Color.hex('c0ffee'));

Colors can be converted from one color space to another by calling the appropriate toXXXColor method on them.

HslColor hsl = new RgbColor(192, 255, 238).toHslColor();

Colors can be altered using a ColorFilter, which will return a new color in the same color space as the input color with that filter applied to it.

RgbColor grey = ColorFilter.greyscale(new RgbColor(192, 255, 238));
HslColor sepia = ColorFilter.sepia(new HslColor(163.8, 100, 87.6));
* Creation of the Color class supporting rgb colors.

* Added hex support and conversion functions.

* Added hsl support and conversion functions.

* Resolved bug where Color.toHexString() was not prepending each byte with 0s as appropriate.
* Added Color.toRgbString() as an alias for Color.toSting().

* Major refactor which will break code using earlier versions.
* Refactored to pull each color space into its own subclass extending a base Color class.

* Added XYZ color space support with bidirectional conversions between XYZ and RGB color spaces.

* Added CIELAB color space support with bidirectional conversions with the XYZ color space (and the RGB color space using XYZ as an intermediary).

* Added toXXXColor() conversion methods for all possible color space transitions.
* Added Color.clone() method to make a copy of a Color object.
* Added toCssString() method to RGB, Hex, and HSL colors for easier use with CSS.
* Added ColorFilters to alter colors according to various filtering rules.
* Added a demo page to test the package and see the effect of filters.

* Color and all subclasses now implement a const constructor.
* Colors are no longer mutable, and all conversion and filter methods return new Color objects.
* Removed the Color.clone() method (this was only generally useful when Colors were mutable).
* Adjusted the greyscale and invert ColorFilters to use the CIELAB color space, giving better results.
* Added a lighten and darken ColorFilter.
* Added an constructor to create colors by CSS3 name.
* Added a Color.toHexColor() method for easier conversion to the HexColor class.
* Added a CssColorSpace interface for the color spaces which have representation in the CSS standard (RGB, HEX, and HSL).

* Removed a switch statement that was preventing dart2js compilation.


import 'dart:html';
import 'package:color/color.dart';

Element body = querySelector('body');

main() {
  addGrid('Sepia', ColorFilter.sepia);
  addGrid('Greyscale', ColorFilter.greyscale);
  addGrid('Inverted', ColorFilter.invert);
  addGrid('Lightened (20%)', ColorFilter.lighten, [.2]);
  addGrid('Darkened (20%)', ColorFilter.darken, [.2]);

addGrid(String name, [ColorFilter filter, List filterArgs = const []]) {
  Element grid = new Element.div()..classes.add('grid');
  num step = 15.9375;
  for (num l = 0; l <= 100; l += step * 100 / 255) {
    Element block = new Element.div()..classes.add('block');
    for (num a = -128; a <= 127; a += step) {
      for (num b = -128; b <= 127; b += step) {
        CielabColor color = new CielabColor(l, a, b);
        if (filter != null) {
          color = filter(color, filterArgs);
        block.append(new Element.div()
 = color.toRgbColor().toCssString());

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  color: ^2.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:color/color.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Feb 25, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/rgb_color.dart. (-25 points)

Analysis of lib/rgb_color.dart reported 157 hints, including:

line 14 col 3: Prefer using /// for doc comments.

line 22 col 18: Don't type annotate initializing formals.

line 22 col 30: Don't type annotate initializing formals.

line 22 col 42: Don't type annotate initializing formals.

line 28 col 13: Unnecessary new keyword.

Fix lib/color_filter.dart. (-9.54 points)

Analysis of lib/color_filter.dart reported 20 hints, including:

line 9 col 15: Don't type annotate initializing formals.

line 9 col 51: Don't type annotate initializing formals.

line 11 col 3: Prefer using /// for doc comments.

line 14 col 32: Unnecessary new keyword.

line 17 col 25: Use isNotEmpty instead of length

Fix lib/color_parser.dart. (-6.78 points)

Analysis of lib/color_parser.dart reported 14 hints, including:

line 5 col 37: Unnecessary new keyword.

line 6 col 41: Unnecessary new keyword.

line 7 col 52: Unnecessary new keyword.

line 8 col 49: Unnecessary new keyword.

line 9 col 49: Unnecessary new keyword.

Fix additional 6 files with analysis or formatting issues. (-9.43 points)

Additional issues in the following files:

  • lib/xyz_color.dart (6 hints)
  • lib/hsl_color.dart (5 hints)
  • lib/cielab_color.dart (4 hints)
  • lib/color.dart (2 hints)
  • lib/hex_color.dart (2 hints)
  • lib/css_color_space.dart (Run dartfmt to format lib/css_color_space.dart.)

Maintenance suggestions

Package is getting outdated. (-38.36 points)

The package was last published 72 weeks ago.

Homepage URL is insecure. (-5 points)

Update the homepage field and use a secure (https) URL.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.61.0 <3.0.0
Dev dependencies
build_runner ^0.10.0
build_test ^0.10.2
build_web_compilers ^0.4.0
test ^1.0.0