Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update image and approx crate dependency #104

Merged
merged 5 commits into from May 28, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions palette/Cargo.toml
Expand Up @@ -23,7 +23,7 @@ strict = []
[dependencies]
palette_derive = {version = "0.4.0", path = "../palette_derive"}
num-traits = "0.2"
approx = "0.1"
approx = "0.2"

[dependencies.phf]
version = "0.7"
Expand All @@ -36,7 +36,7 @@ features = ["serde_derive"]
optional = true

[dev-dependencies]
image = "0.18"
image = "0.19"
clap = "2"
csv = "1.0.0-beta.3"
serde = "1"
Expand Down
24 changes: 15 additions & 9 deletions palette/examples/color_scheme.rs
Expand Up @@ -190,15 +190,21 @@ fn blit_shades<I: GenericImage<Pixel = image::Rgb<u8>> + 'static>(
.into_format()
.into_raw();

for (x, y, pixel) in canvas.pixels_mut() {
if y < height / 2 {
pixel.data = primary;
} else if x < width / 3 {
pixel.data = light;
} else if x < (width / 3) * 2 {
pixel.data = dark1;
} else {
pixel.data = dark2;

for x in 0..width {
for y in 0..height {
canvas.put_pixel(x, y, image::Rgb {
data:
if y < height / 2 {
primary
} else if x < width / 3 {
light
} else if x < (width / 3) * 2 {
dark1
} else {
dark2
}
});
}
}
}
49 changes: 41 additions & 8 deletions palette/examples/gradient.rs
Expand Up @@ -49,20 +49,53 @@ fn main() {
let c3 = Srgb::from_linear(c3.into()).into_format().into_raw();
let c4 = Srgb::from_linear(c4.into()).into_format().into_raw();

for (_, _, pixel) in image.sub_image(i as u32, 0, 1, 31).pixels_mut() {
pixel.data = c1

{
let mut sub_image = image.sub_image(i as u32, 0, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c1
});
}
}
}

for (_, _, pixel) in image.sub_image(i as u32, 32, 1, 31).pixels_mut() {
pixel.data = c2;
{
let mut sub_image = image.sub_image(i as u32, 32, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c2
});
}
}
}

for (_, _, pixel) in image.sub_image(i as u32, 65, 1, 31).pixels_mut() {
pixel.data = c3;
{
let mut sub_image = image.sub_image(i as u32, 65, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c3
});
}
}
}

for (_, _, pixel) in image.sub_image(i as u32, 97, 1, 31).pixels_mut() {
pixel.data = c4;
{
let mut sub_image = image.sub_image(i as u32, 97, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c4
});
}
}
}
}

Expand Down
41 changes: 30 additions & 11 deletions palette/examples/readme_examples.rs
Expand Up @@ -66,8 +66,12 @@ mod gradients {
fn display_colors(filename: &str, colors: &[Srgb<u8>]) {
let mut image = RgbImage::new(colors.len() as u32 * 64, 64);
for (i, &color) in colors.iter().enumerate() {
for (_, _, pixel) in image.sub_image(i as u32 * 64, 0, 64, 64).pixels_mut() {
pixel.data = *color.as_raw();
let mut sub_image = image.sub_image(i as u32 * 64, 0, 64, 64);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: *color.as_raw() });
}
}
}

Expand All @@ -86,17 +90,32 @@ fn display_gradients<A: Mix<Scalar = f32> + Clone, B: Mix<Scalar = f32> + Clone>
LinSrgb: From<B>,
{
let mut image = RgbImage::new(256, 64);

for (x, _, pixel) in image.sub_image(0, 0, 256, 32).pixels_mut() {
pixel.data = Srgb::from_linear(grad1.get(x as f32 / 255.0).into())
.into_format()
.into_raw();
{
let mut sub_image = image.sub_image(0, 0, 256, 32);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: Srgb::from_linear(grad1.get(x as f32 / 255.0).into())
.into_format()
.into_raw()
});
}
}
}

for (x, _, pixel) in image.sub_image(0, 32, 256, 32).pixels_mut() {
pixel.data = Srgb::from_linear(grad2.get(x as f32 / 255.0).into())
.into_format()
.into_raw();
{
let mut sub_image = image.sub_image(0, 32, 256, 32);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: Srgb::from_linear(grad2.get(x as f32 / 255.0).into())
.into_format()
.into_raw()
});
}
}
}

match image.save(filename) {
Expand Down
49 changes: 31 additions & 18 deletions palette/examples/saturate.rs
Expand Up @@ -15,27 +15,40 @@ fn main() {

//Increase the saturation by 80% (!) as HSL in the left half, and as LCh
//in the right half. Notice the strong yellow tone in the HSL part.
for (_, _, pixel) in image.sub_image(0, 0, width / 2, height).pixels_mut() {
let color: Hsl = Srgb::from_raw(&pixel.data)
.into_format()
.into_linear()
.into();

let saturated = color.saturate(0.8);
pixel.data = Srgb::from_linear(saturated.into()).into_format().into_raw();
{
let mut sub_image = image.sub_image(0, 0, width / 2, height);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let color: Hsl = Srgb::from_raw(&sub_image.get_pixel(x, y).data)
.into_format()
.into_linear()
.into();

let saturated = color.saturate(0.8);
sub_image.put_pixel(x, y, image::Rgb {
data: Srgb::from_linear(saturated.into()).into_format().into_raw()
});
}
}
}

for (_, _, pixel) in image
.sub_image(width / 2, 0, width / 2, height)
.pixels_mut()
{
let color: Lch = Srgb::from_raw(&pixel.data)
.into_format()
.into_linear()
.into();

let saturated = color.saturate(0.8);
pixel.data = Srgb::from_linear(saturated.into()).into_format().into_raw();
let mut sub_image = image.sub_image(width / 2, 0, width / 2, height);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let color: Lch = Srgb::from_raw(&sub_image.get_pixel(x, y).data)
.into_format()
.into_linear()
.into();

let saturated = color.saturate(0.8);
sub_image.put_pixel(x, y, image::Rgb {
data: Srgb::from_linear(saturated.into()).into_format().into_raw()
});
}
}
}

match image.save("examples/saturate.png") {
Expand Down
60 changes: 48 additions & 12 deletions palette/examples/shade.rs
Expand Up @@ -21,12 +21,24 @@ fn main() {
.into_format()
.into_raw();

for (_, _, pixel) in image.sub_image(i as u32 * 20, 0, 20, 31).pixels_mut() {
pixel.data = rgb1;
{
let mut sub_image = image.sub_image(i as u32 * 20, 0, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: rgb1 });
}
}
}

for (_, _, pixel) in image.sub_image(i as u32 * 20, 32, 20, 31).pixels_mut() {
pixel.data = rgb2;
{
let mut sub_image = image.sub_image(i as u32 * 20, 32, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: rgb2 });
}
}
}

let lab1 = Srgb::from_linear(lab.darken(0.05 * i as f32).into())
Expand All @@ -36,12 +48,24 @@ fn main() {
.into_format()
.into_raw();

for (_, _, pixel) in image.sub_image(i as u32 * 20, 65, 20, 31).pixels_mut() {
pixel.data = lab1;
{
let mut sub_image = image.sub_image(i as u32 * 20, 65, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: lab1 });
}
}
}

for (_, _, pixel) in image.sub_image(i as u32 * 20, 97, 20, 31).pixels_mut() {
pixel.data = lab2;
{
let mut sub_image = image.sub_image(i as u32 * 20, 97, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: lab2 });
}
}
}

let hsv1 = Srgb::from_linear(hsv.darken(0.05 * i as f32).into())
Expand All @@ -51,12 +75,24 @@ fn main() {
.into_format()
.into_raw();

for (_, _, pixel) in image.sub_image(i as u32 * 20, 130, 20, 31).pixels_mut() {
pixel.data = hsv1;
{
let mut sub_image = image.sub_image(i as u32 * 20, 130, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: hsv1 });
}
}
}

for (_, _, pixel) in image.sub_image(i as u32 * 20, 162, 20, 31).pixels_mut() {
pixel.data = hsv2;
{
let mut sub_image = image.sub_image(i as u32 * 20, 162, 20, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb { data: hsv2 });
}
}
}
}

Expand Down
35 changes: 27 additions & 8 deletions palette/src/alpha.rs
Expand Up @@ -3,7 +3,7 @@ use std::fmt;

use num_traits::Float;

use approx::ApproxEq;
use approx::{AbsDiffEq, RelativeEq, UlpsEq};

use {clamp, Blend, Component, ComponentWise, GetHue, Hue, Limited, Mix, Pixel, Saturate, Shade};
use blend::PreAlpha;
Expand Down Expand Up @@ -159,10 +159,10 @@ impl<C: Default, T: Component> Default for Alpha<C, T> {
}
}

impl<C, T> ApproxEq for Alpha<C, T>
impl<C, T> AbsDiffEq for Alpha<C, T>
where
C: ApproxEq<Epsilon = T::Epsilon>,
T: ApproxEq,
C: AbsDiffEq<Epsilon = T::Epsilon>,
T: AbsDiffEq,
T::Epsilon: Clone,
{
type Epsilon = T::Epsilon;
Expand All @@ -171,12 +171,20 @@ where
T::default_epsilon()
}

fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
self.color.abs_diff_eq(&other.color, epsilon.clone())
&& self.alpha.abs_diff_eq(&other.alpha, epsilon)
}
}

fn default_max_ulps() -> u32 {
T::default_max_ulps()
impl<C, T> RelativeEq for Alpha<C, T>
where
C: RelativeEq<Epsilon = T::Epsilon>,
T: RelativeEq,
T::Epsilon: Clone,
{
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
}

fn relative_eq(
Expand All @@ -189,6 +197,17 @@ where
.relative_eq(&other.color, epsilon.clone(), max_relative.clone())
&& self.alpha.relative_eq(&other.alpha, epsilon, max_relative)
}
}

impl<C, T> UlpsEq for Alpha<C, T>
where
C: UlpsEq<Epsilon = T::Epsilon>,
T: UlpsEq,
T::Epsilon: Clone,
{
fn default_max_ulps() -> u32 {
T::default_max_ulps()
}

fn ulps_eq(&self, other: &Alpha<C, T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.color.ulps_eq(&other.color, epsilon.clone(), max_ulps)
Expand Down