3个版本
使用旧的Rust 2015
0.1.2 | 2015年4月3日 |
---|---|
0.1.1 | 2015年3月27日 |
0.1.0 | 2015年3月20日 |
#861 in 图像
51KB
866 行
glm_color
一个简单的操纵和生成颜色值的库。
这是glm-rs的一个扩展。
用法
将glm_color
添加到Cargo.toml
的依赖中。例如,
[dependencies]
glm_color = "*"
许可证
MIT许可证(MIT)。
lib.rs
:
一个简单的crate,用于操纵和生成颜色值。它是glm
crate的扩展。
glm_color
将颜色值视为数字,而不是可以直接用于渲染的东西。因此,数据格式、通道顺序,甚至是alpha通道,都不由这个库处理。
这个crate中最有趣的部分是Rgb
和Hsv
颜色空间中的函数,它们可以生成颜色。这些函数的设计基于维基百科页面色彩理论和这个博客。
示例
生成颜色
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);
操纵颜色
use glm_color::*;
// Linear RGB color space supports some arithmetics.
let yellow = RED + GREEN;
let white = yellow + BLUE;
assert_eq!(white, WHITE);
转换颜色
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);
依赖项
~5MB
~94K SLoC