color 2.1.1

  • README.md
  • changelog
  • Example
  • Installing
  • Versions
  • 93

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:
dependencies:
  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 RgbColor.name('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));
0.1.0
-----
* Creation of the Color class supporting rgb colors.

0.2.0
-----
* Added hex support and conversion functions.

0.3.0
-----
* Added hsl support and conversion functions.

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

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

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

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

1.3.0
-----
* 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.

2.0.0
-----
* 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 RgbColor.name() 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).

2.0.1
-----
* Removed a switch statement that was preventing dart2js compilation.

example/example.dart

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

Element body = querySelector('body');

main() {
  addGrid('Normal');
  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 []]) {
  body.appendText(name);
  Element grid = new Element.div()..classes.add('grid');
  body.append(grid);
  num step = 15.9375;
  for (num l = 0; l <= 100; l += step * 100 / 255) {
    Element block = new Element.div()..classes.add('block');
    grid.append(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()
          ..classes.add('color-block')
          ..style.background = color.toRgbColor().toCssString());
      }
    }
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  color: ^2.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
2.1.1 Oct 8, 2018 Go to the documentation of color 2.1.1 Download color 2.1.1 archive
2.0.2 Jul 3, 2017 Go to the documentation of color 2.0.2 Download color 2.0.2 archive
2.0.1 Mar 14, 2017 Go to the documentation of color 2.0.1 Download color 2.0.1 archive
2.0.0 Jun 24, 2015 Go to the documentation of color 2.0.0 Download color 2.0.0 archive
1.3.0 Apr 3, 2015 Go to the documentation of color 1.3.0 Download color 1.3.0 archive
1.2.0 Jan 21, 2015 Go to the documentation of color 1.2.0 Download color 1.2.0 archive
0.3.1 May 27, 2014 Go to the documentation of color 0.3.1 Download color 0.3.1 archive
0.3.0 Jan 30, 2014 Go to the documentation of color 0.3.0 Download color 0.3.0 archive
0.2.0 Jan 27, 2014 Go to the documentation of color 0.2.0 Download color 0.2.0 archive
0.1.0 Jan 26, 2014 Go to the documentation of color 0.1.0 Download color 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
86
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
93
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:color/color.dart.

Health suggestions

Format lib/cielab_color.dart.

Run dartfmt to format lib/cielab_color.dart.

Format lib/color.dart.

Run dartfmt to format lib/color.dart.

Format lib/color_filter.dart.

Run dartfmt to format lib/color_filter.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/color_parser.dart (Run dartfmt to format lib/color_parser.dart.)
  • lib/css_color_space.dart (Run dartfmt to format lib/css_color_space.dart.)
  • lib/hex_color.dart (Run dartfmt to format lib/hex_color.dart.)

Dependencies

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