4 releases

0.3.3 Aug 2, 2022
0.3.2 Aug 2, 2022
0.3.1 Jul 31, 2022
0.2.0 Jul 29, 2022
0.1.0 Jul 29, 2022

#1139 in Algorithms

49 downloads per month

MIT license

46KB
858 lines

octarine

Converts colors to many common color representations (e.g. RGB, HSL, HSV) and does simple colour manipulation operations. Thanks to colour and colors.py for inspiring the API (and documentation) of this project.

Note

Color stores colors using the RGB color model, storing R, G, and B values as u8s. Hence, the result you get back from converting from HSL/HSV to Color and back will not always be the same due to lack of precision.

use octarine::Color;

let to_hsl = Color::from_hsl(0.0, 0.0, 0.5).to_hsl();
assert_ne!((0.0, 0.0, 0.5), to_hsl);

let to_hsv = Color::from_hsv(0.0, 0.0, 0.5).to_hsv();
assert_ne!((0.0, 0.0, 0.5), to_hsv);

Features

  • Extremely simple API (subjective).
  • Convert between RGB, HSL, HSV, W3C web colors, and hexadecimal.
  • One struct Color to rule them all.
  • Perform arithmetic, blend modes, and generate random colors within boundaries.
  • Octarine.

Examples

use octarine::Color;

let color1 = Color::from_web_color("red");
let color2 = Color::new(255, 0, 0);
assert_eq!(color1, Some(color2.clone()));

let hex = Color::new(100, 100, 100).to_hex();
assert_eq!(hex, 0x646464);

let red = color2.get_red();
let green = color2.get_green();
let blue = color2.get_blue();
assert_eq!(color2.to_rgb(), (red, green, blue));

let hue = color2.get_hsl_hue();
let saturation = color2.get_hsl_saturation();
let luminance = color2.get_hsl_luminance();
assert_eq!(color2.to_hsl(), (hue, saturation, luminance));

let hue = color2.get_hsv_hue();
let saturation = color.get_hsv_saturation();
let value = color.get_hsv_value();
assert_eq!(color2.to_hsv(), (hue, saturation, value));

Dependencies

~0.7–1.2MB
~26K SLoC