Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Jan 23, 2016
1 parent 398bde6 commit 38a06a4
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 357 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ extern crate palette;
use palette::{Rgb, Hsv, Gradient};

let grad1 = Gradient::new(vec![
Rgb::linear_rgb(1.0, 0.1, 0.1),
Rgb::linear_rgb(0.1, 1.0, 1.0)
Rgb::linear(1.0, 0.1, 0.1),
Rgb::linear(0.1, 1.0, 1.0)
]);

let grad2 = Gradient::new(vec![
Hsv::from(Rgb::linear_rgb(1.0, 0.1, 0.1)),
Hsv::from(Rgb::linear_rgb(0.1, 1.0, 1.0))
Hsv::from(Rgb::linear(1.0, 0.1, 0.1)),
Hsv::from(Rgb::linear(0.1, 1.0, 1.0))
]);
```

Expand Down
2 changes: 1 addition & 1 deletion examples/color_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn main() {
.and_then(|r| r.parse().ok())
.expect("the blue channel must be a number in the range [0-255]");

let primary = Color::srgb8(red, green, blue);
let primary = Color::srgb_u8(red, green, blue);

//Generate the secondary colors, depending on the input arguments
let secondary = match matches.subcommand() {
Expand Down
24 changes: 12 additions & 12 deletions examples/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ use image::{RgbImage, GenericImage};
fn main() {
//A gradient of evenly spaced colors
let grad1 = Gradient::new(vec![
Rgb::linear_rgb(1.0, 0.1, 0.1),
Rgb::linear_rgb(0.1, 0.1, 1.0),
Rgb::linear_rgb(0.1, 1.0, 0.1)
Rgb::linear(1.0, 0.1, 0.1),
Rgb::linear(0.1, 0.1, 1.0),
Rgb::linear(0.1, 1.0, 0.1)
]);

//The same colors as in grad1, but with the blue point shifted down
let grad2 = Gradient::with_domain(vec![
(0.0, Rgb::linear_rgb(1.0, 0.1, 0.1)),
(0.25, Rgb::linear_rgb(0.1, 0.1, 1.0)),
(1.0, Rgb::linear_rgb(0.1, 1.0, 0.1))
(0.0, Rgb::linear(1.0, 0.1, 0.1)),
(0.25, Rgb::linear(0.1, 0.1, 1.0)),
(1.0, Rgb::linear(0.1, 1.0, 0.1))
]);

//The same colors and offsets as in grad1, but in a color space where the hue is a component
let grad3 = Gradient::new(vec![
Lch::from(Rgb::linear_rgb(1.0, 0.1, 0.1)),
Lch::from(Rgb::linear_rgb(0.1, 0.1, 1.0)),
Lch::from(Rgb::linear_rgb(0.1, 1.0, 0.1))
Lch::from(Rgb::linear(1.0, 0.1, 0.1)),
Lch::from(Rgb::linear(0.1, 0.1, 1.0)),
Lch::from(Rgb::linear(0.1, 1.0, 0.1))
]);

//The same colors and and color space as in grad3, but with the blue point shifted down
let grad4 = Gradient::with_domain(vec![
(0.0, Lch::from(Rgb::linear_rgb(1.0, 0.1, 0.1))),
(0.25, Lch::from(Rgb::linear_rgb(0.1, 0.1, 1.0))),
(1.0, Lch::from(Rgb::linear_rgb(0.1, 1.0, 0.1)))
(0.0, Lch::from(Rgb::linear(1.0, 0.1, 0.1))),
(0.25, Lch::from(Rgb::linear(0.1, 0.1, 1.0))),
(1.0, Lch::from(Rgb::linear(0.1, 1.0, 0.1)))
]);

let mut image = RgbImage::new(256, 128);
Expand Down
8 changes: 4 additions & 4 deletions examples/readme_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ mod gradients {

pub fn run() {
let grad1 = Gradient::new(vec![
Rgb::linear_rgb(1.0, 0.1, 0.1),
Rgb::linear_rgb(0.1, 1.0, 1.0)
Rgb::linear(1.0, 0.1, 0.1),
Rgb::linear(0.1, 1.0, 1.0)
]);

let grad2 = Gradient::new(vec![
Hsv::from(Rgb::linear_rgb(1.0, 0.1, 0.1)),
Hsv::from(Rgb::linear_rgb(0.1, 1.0, 1.0))
Hsv::from(Rgb::linear(1.0, 0.1, 0.1)),
Hsv::from(Rgb::linear(0.1, 1.0, 1.0))
]);

display_gradients("examples/readme_gradients.png", grad1, grad2);
Expand Down
2 changes: 1 addition & 1 deletion examples/shade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use image::{RgbImage, GenericImage};

fn main() {
//The same color in linear RGB, CIE L*a*b*, and HSV
let rgb = Rgb::linear_rgb(0.5, 0.0, 0.0);
let rgb = Rgb::linear(0.5, 0.0, 0.0);
let lab = Lab::from(rgb);
let hsv = Hsv::from(rgb);

Expand Down
185 changes: 185 additions & 0 deletions src/alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
use std::ops::{Deref, DerefMut, Add, Sub, Mul, Div};

use num::Float;

use {Mix, Shade, GetHue, Hue, Saturate};

///An alpha channel wrapper for colors.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Alpha<C, T: Float> {
///Any color.
pub color: C,
///The bolted on alpha channel.
pub alpha: T,
}

impl<C, T: Float> Deref for Alpha<C, T> {
type Target = C;

fn deref(&self) -> &C {
&self.color
}
}

impl<C, T: Float> DerefMut for Alpha<C, T> {
fn deref_mut(&mut self) -> &mut C {
&mut self.color
}
}

impl<C: Mix<T>, T: Float> Mix<T> for Alpha<C, T> {
fn mix(&self, other: &Alpha<C, T>, factor: T) -> Alpha<C, T> {
Alpha {
color: self.color.mix(&other.color, factor),
alpha: self.alpha + factor * (other.alpha - self.alpha),
}
}
}

impl<C: Shade<T>, T: Float> Shade<T> for Alpha<C, T> {
fn lighten(&self, amount: T) -> Alpha<C, T> {
Alpha {
color: self.color.lighten(amount),
alpha: self.alpha,
}
}
}

impl<C: GetHue, T: Float> GetHue for Alpha<C, T> {
type Hue = C::Hue;

fn get_hue(&self) -> Option<C::Hue> {
self.color.get_hue()
}
}

impl<C: Hue, T: Float> Hue for Alpha<C, T> {
fn with_hue(&self, hue: C::Hue) -> Alpha<C, T> {
Alpha {
color: self.color.with_hue(hue),
alpha: self.alpha,
}
}

fn shift_hue(&self, amount: C::Hue) -> Alpha<C, T> {
Alpha {
color: self.color.shift_hue(amount),
alpha: self.alpha,
}
}
}

impl<C: Saturate<T>, T: Float> Saturate<T> for Alpha<C, T> {
fn saturate(&self, factor: T) -> Alpha<C, T> {
Alpha {
color: self.color.saturate(factor),
alpha: self.alpha,
}
}
}

impl<C: Default, T: Float> Default for Alpha<C, T> {
fn default() -> Alpha<C, T> {
Alpha {
color: C::default(),
alpha: T::one(),
}
}
}

impl<C: Add, T: Float> Add for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn add(self, other: Alpha<C, T>) -> Alpha<C::Output, T> {
Alpha {
color: self.color + other.color,
alpha: self.alpha + other.alpha,
}
}
}

impl<T: Float + Clone, C: Add<T>> Add<T> for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn add(self, c: T) -> Alpha<C::Output, T> {
Alpha {
color: self.color + c.clone(),
alpha: self.alpha + c,
}
}
}

impl<C: Sub, T: Float> Sub for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn sub(self, other: Alpha<C, T>) -> Alpha<C::Output, T> {
Alpha {
color: self.color - other.color,
alpha: self.alpha - other.alpha,
}
}
}

impl<T: Float + Clone, C: Sub<T>> Sub<T> for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn sub(self, c: T) -> Alpha<C::Output, T> {
Alpha {
color: self.color - c.clone(),
alpha: self.alpha - c,
}
}
}

impl<C: Mul, T: Float> Mul for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn mul(self, other: Alpha<C, T>) -> Alpha<C::Output, T> {
Alpha {
color: self.color * other.color,
alpha: self.alpha * other.alpha,
}
}
}

impl<T: Float + Clone, C: Mul<T>> Mul<T> for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn mul(self, c: T) -> Alpha<C::Output, T> {
Alpha {
color: self.color * c.clone(),
alpha: self.alpha * c,
}
}
}

impl<C: Div, T: Float> Div for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn div(self, other: Alpha<C, T>) -> Alpha<C::Output, T> {
Alpha {
color: self.color / other.color,
alpha: self.alpha / other.alpha,
}
}
}

impl<T: Float + Clone, C: Div<T>> Div<T> for Alpha<C, T> {
type Output = Alpha<C::Output, T>;

fn div(self, c: T) -> Alpha<C::Output, T> {
Alpha {
color: self.color / c.clone(),
alpha: self.alpha / c,
}
}
}

impl<C, T: Float> From<C> for Alpha<C, T> {
fn from(color: C) -> Alpha<C, T> {
Alpha {
color: color,
alpha: T::one(),
}
}
}
2 changes: 1 addition & 1 deletion src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ mod test {

#[test]
fn simple_slice() {
let g1 = Gradient::new(vec![Rgb::linear_rgb(1.0, 0.0, 0.0), Rgb::linear_rgb(0.0, 0.0, 1.0)]);
let g1 = Gradient::new(vec![Rgb::linear(1.0, 0.0, 0.0), Rgb::linear(0.0, 0.0, 1.0)]);
let g2 = g1.slice(..0.5);

let v1: Vec<_> = g1.take(10).take(5).collect();
Expand Down
Loading

0 comments on commit 38a06a4

Please sign in to comment.