#color #glm #graphics

glm_color

A simple library for manipulating and generating color values, based on the glm-rs math library

3 releases

Uses old Rust 2015

0.1.2 Apr 3, 2015
0.1.1 Mar 27, 2015
0.1.0 Mar 20, 2015

#628 in Graphics APIs

21 downloads per month

MIT license

51KB
866 lines

glm_color

A simple library for manipulating and generating color values.

This is an extension of glm-rs.

Usage

Add glm_color to the dependencies of Cargo.toml. For example,

[dependencies]
glm_color = "*"

License

MIT License (MIT).


lib.rs:

A simple crate for manipulating and generating color values. It is an extension to the glm crate.

glm_color treats color values as numbers, instead of somthing that can be used in rendering directly. So things like data format, order of channels, and even alpha channel, are not handled by this library.

The only interesting part of this crate is functions in Rgb and Hsv color spaces that produce colors procedurally. The design of these functions are based on Wikipedia page Color Theory and this blog.

Example

Generating colors

use glm::*;
use glm::ext::tau;
use glm_color::*;

// constant color values.
let mut red = RED;

// with constructors.
red = Rgb::new(1., 0., 0.);
red = rgb(255, 0, 0);

// from other color spaces.
red = hsv(tau(), 1., 1.).to_rgb();
red = ycbcr(1., 0., 0.5).to_rgb();

// randomly.
let rnd = Rgb::rand();
let rnd_hsv = Hsv::rand();

// procedurally.
let blues = from_rgb::<Hsv>(BLUE).analogs(5, radians(30.));
let yellows: Vec<Hsv> = blues.iter().map(|clr| -> Hsv {
    clr.complement()
}).collect();
let darker_red = from_rgb::<Hsv>(RED).shade(0.3);

Manipulating colors

use glm_color::*;

// Linear RGB color space supports some arithmetics.
let yellow = RED + GREEN;
let white = yellow + BLUE;
assert_eq!(white, WHITE);

Converting colors

use glm::*;
use glm_color::*;

let rgb = RED;
// All color spaces can be converted to and from the linear RGB color space.
let hsv = Hsv::from_rgb(rgb);
let mut red = hsv.to_rgb();
assert!(is_close_to(&rgb, &red, 0.000001));
let ybr: YCbCr = from_rgb(rgb);
red = to_rgb(&ybr);
let srgb = Srgb::from_rgb(rgb);

Dependencies

~5MB
~94K SLoC