Module truster::color[][src]

Expand description

An RGB color representation. The components are floating point numbers. Support for generics may be added in the future. When used, the components should be clamped between 0 and 1.

Examples

You can create colors Color::new:

let c = Color::new(-0.5, 0.4, 1.7);
assert_eq!(c.r(), -0.5);
assert_eq!(c.g(), 0.4);
assert_eq!(c.b(), 1.7);

Individual components can be accessed with their respective methods, or with indexing:

… or with indexing:

let c = Color::new(1.0, 4.2, -3.7);
assert_eq!(c[0], 1.0);
assert_eq!(c[1], 4.2);
assert_eq!(c[2], -3.7);

Arithmetic

Colors support all common arithmetic operations. All operations which support operator overloading support mutable assignment. The available operations are:

  • Addition
let mut c1 = Color::new(3.0, -2.0, 5.0);
let c2 = Color::new(-2.0, 3.0, 1.0);
assert_eq!(c1 + c2, Color::new(1.0, 1.0, 6.0));
c1 += c2;
assert_eq!(c1, Color::new(1.0, 1.0, 6.0));
  • Subtraction
let c1 = Color::new(3.0, 2.0, 1.0);
let c2 = Color::new(5.0, 6.0, 7.0);
assert_eq!(c1 - c2, Color::new(-2.0, -4.0, -6.0));
  • Scalar multiplication
let c = Color::new(1.0, -2.0, 3.0);
assert_eq!(c * 3.5, Color::new(3.5, -7.0, 10.5));
assert_eq!(c * 0.5, Color::new(0.5, -1.0, 1.5));
  • Hadamard multiplication
let c1 = Color::new(1.0, 0.2, 0.4);
let c2 = Color::new(0.9, 1.0, 0.1);
assert_eq!(c1 * c2, Color::new(0.9, 0.2, 0.04000000000000001));

Structs

Represents an RGB color. See the module’s documentation for more info.