color_models 0.2.6+2

  • Readme
  • Changelog
  • Installing
  • 79

color_models #

pub package style: effective dart

A package with models for defining colors in the CMYK, HSI, HSL, HSP, HSV, LAB, RGB, and XYZ color spaces, with methods for converting colors between spaces and adjusting their properties.

For use with Flutter, see: https://pub.dev/packages/flutter_color_models

Check out palette, a package for creating, generating, and modifying color palettes.

Usage #

import 'package:color_models/color_models.dart';

Color Spaces #

color_models exposes models for the CMYK, HSI, HSL, HSP, HSV, LAB, RGB, and XYZ color spaces; represented as [CmykColor], [HsiColor], [HslColor], [HspColor], [HsvColor], [LabColor], [RgbColor], and [XyzColor] respectively.

Each model is constant and extends [ColorModel].

Creating Colors #

Colors can be created by constructing a [ColorModel] directly, or with the [fromList] or [extrapolate] constructors. [extrapolate] accepts each model's respective values on a 0 to 1 scale, and extrapolates them to their normal scale.

Each model posesses values for each property of their respective acronyms, as well as an optional [alpha] value.

// Each of the below colors is red at 100% opacity.

// RGB without alpha
RgbColor(255, 0, 0);
RgbColor.fromList(<num>[255, 0, 0]);
RgbColor.extrapolate(<num>[1.0, 0.0, 0.0]);

// RGB with alpha
RgbColor(255, 0, 0, 1.0);
RgbColor.fromList(<num>[255, 0, 0, 1.0]);
RgbColor.extrapolate(<num>[1.0, 0.0, 0.0, 1.0]);

// CMYK without alpha
CmykColor(0.0, 100.0, 100.0, 0.0);
CmykColor.fromList(<num>[0.0, 100.0, 100.0, 0.0]);
CmykColor.extrapolate(<num>[0.0, 1.0, 1.0, 0.0]);

// CMYK with alpha
CmykColor(0.0, 100.0, 100.0, 0.0, 1.0);
CmykColor.fromList(<num>[0.0, 100.0, 100.0, 0.0, 1.0]);
CmykColor.extrapolate(<num>[0.0, 1.0, 1.0, 0.0, 1.0]);

// HSL without alpha
HslColor(0.0, 100.0, 50.0);
HslColor.fromList(<num>[0.0, 100.0, 50.0]);
HslColor.extrapolate(<num>[0.0, 1.0, 0.5]);

// HSL with alpha
HslColor(0.0, 100.0, 50.0, 1.0);
HslColor.fromList(<num>[0.0, 100.0, 50.0, 1.0]);
HslColor.extrapolate(<num>[0.0, 1.0, 0.5, 1.0]);

Converting Colors Between Spaces #

Each color model has methods to convert itself to every other color model.

CmykColor toCmykColor();

HsiColor toHsiColor();

HslColor toHslColor();

HspColor toHspColor();

HsvColor toHsvColor();

LabColor toLabColor();

RgbColor toRgbColor();

XyzColor toXyzColor();

Altenatively, each color model has a constructor [ColorModel.from] that returns a color from any color space in the color space being called.

// Create a HSV color
var originalColor = HsvColor(0.3, 0.8, 0.7);

// Convert it to RGB => RgbColor(64, 179, 36)
var rgbColor = RgbColor.from(originalColor);

// Then to CMYK => CmykColor(64.25, 0, 79.89, 29.8)
var cmykColor = CmykColor.from(rgbColor);

// Back to HSV => HsvColor(0.3, 0.8, 0.7)
var hsvColor = HsvColor.from(cmykColor);

Note: All color conversions use the RGB color space as an intermediary. To minimize the loss of precision when converting between other color spaces, [RgbColor] privately stores the RGB values as [num]s rather than [int]s. The [num] values can be returned as a list with [RgbColor]'s toPreciseList() method.

Color Adjustments #

For convenience, each [ColorModel] has 2 getters, [inverted] and [opposite], and 3 methods, [cooler], [warmer] and [rotateHue], for generating new colors.

[inverted] inverts the colors properties within their respective ranges, excluding hue, which is instead rotated 180 degrees.

final orange = RgbColor(255, 144, 0);

print(orange.inverted); // RgbColor(0, 111, 255);

[opposite] returns the color with the hue opposite this. [opposite] is shorthand for color.rotateHue(180).

[rotateHue] rotates the hue of the color by the [amount] specified in degrees. Colors in the CMYK, LAB, RGB, and XYZ color spaces will be converted to and from the HSL color space where the hue will be rotated.

final orange = RgbColor(255, 144, 0);

print(orange.opposite); // RgbColor(0, 111, 255);

print(orange.rotateHue(30)); // RgbColor(239, 255, 0);

print(orange.rotateHue(-30)); // RgbColor(255, 17, 0);

[warmer] and [cooler] will rotate the hue of the color by the [amount] specified towards 90 degrees and 270 degrees, respectively. The hue's value will be capped at 90 or 270.

final orange = RgbColor(255, 144, 0);

print(orange.warmer(30)); // RgbColor(239, 255, 0);

print(orange.cooler(30)); // RgbColor(255, 17, 0);

[0.2.6] - March 24, 2020 #

  • Added the random factory constructor to each [ColorModel].

  • The equality operator and some getters now rounds values to the millionth due to the slight loss of precision during conversions.

  • The [hue] getter now calculates hues directly from RGB, rather than doing a full conversion to HSL.

  • Added the [isMonochromatic] getter to each [ColorModel].

[0.2.5+1] - March 23, 2020 #

  • Fixed a bug in the HSP to RGB conversion method.

[0.2.5] - March 22, 2020 #

  • Added the [relative] parameter to the [warmer] and [cooler] methods.

[0.2.4+2] - March 22, 2020 #

  • Added the [saturation] getter to [ColorModel].

[0.2.4+1] - March 22, 2020 #

  • Added the [hue] getter to [ColorModel].

[0.2.4] - March 21, 2020 #

  • Added the [inverted] and [opposite] getters, as well as the [warmer], [cooler], [rotateHue], and [withHue] methods to each [ColorModel].

[0.2.3] - March 18, 2020 #

  • Added [alpha] values to all [ColorModel]s.

  • Added withValue methods to all [ColorModel]s.

[0.2.2] - March 16, 2020 #

  • Added [hex] getter to [ColorModel].

  • Added [fromHex] static method to all [ColorModel]s.

  • All [ColorModel]s are now @immutable.

[0.2.1] - January 15, 2020 #

  • All [ColorModel]s now @override the toString() method.

[0.2.0+1] - January 15, 2020 #

  • Documentation and formatting changes.

[0.2.0] - January 4, 2020 #

  • To provide better support for 3rd party color models, [ColorModel]s are now requried to @override the [toRgbColor] method. All [ColorModel]s included in this library still use the color conversion methods found in the [ColorConverter] helper class, but the methods are now public.

[0.1.2] - July 31, 2019 #

  • All [ColorModel]s now @override [hashCode].

[0.1.1] - July 25, 2019 #

  • Added a private constructor to the [ColorConverter] class.

  • Minor formatting changes.

[0.1.0] - July 23, 2019 #

  • Initial release

Use this package as a library

1. Depend on it

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


dependencies:
  color_models: ^0.2.6+2

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_models/color_models.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
61
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and color_models.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
meta ^1.1.8 1.1.8
Dev dependencies
effective_dart ^1.2.1
test ^1.6.5