Skip to content

Commit

Permalink
[WIP] Update palette crate to version 0.4.
Browse files Browse the repository at this point in the history
The biggest update to account for here was the distinction between
`RgbSpace`s. Nannou now uses the `Srgb` standard encoding as a default
throughout the crate which should more accurately represents nannou's
provided set of named colors (nannou-org#72) and likely close match users
expectations of the behaviour.

Closes nannou-org#72.
Closes nannou-org#345.

TODO:

- [ ] Decide whether or not to re-add the `Rgb` and `Rgba` types to the
  `color` module? Or should we encourage use of the `Srgb` and `Srgba`
  types instead for clarity?
- [ ] Clarify whether we should be useing `Srgb` or `Linear<Srgb>` RGB
  encoding for nannou's default draw API. I've opened up
  Ogeon/palette#133 to help clarify my understanding on this.
- [ ] Add a suite of super simple constructor functions to the color
  module (e.g. `hsl`, `hsla`, `rgb`, `rgba`, etc) that produce color
  types that are easily compatible with nannou's `Draw` API.
- [ ] Decide if we should keep the Tango based `named` colors provided
  by nannou (see [here][1]) or only use those provided [by palette][2].
  Alternatively we could keep both, but this would mean removing the
  glob palette import to fix nannou-org#345 and re-exporting `palette`'s named
  colors into our own named module.
- [ ] Decide what the most convenient type is for `named` colors. We
  should probably change them to the same type as `palette`'s named
  colors, but also ensure that these can be conveniently used within all
  areas of the `Draw` API.
- [ ] Create a `colors.rs` example that demonstrates all named colors
  within the crate. Ensure that these colors visibly match their
  references.

cc @SjorsVanGelderen.

[1]: /~https://github.com/nannou-org/nannou/blob/master/src/color.rs#L12
[2]: https://docs.rs/palette/0.4.1/palette/named/index.html
  • Loading branch information
mitchmindtree committed Jul 20, 2019
1 parent add5fb2 commit 57170d8
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ daggy = "0.6"
find_folder = "0.3"
image = "0.21"
noise = "0.5"
palette = "0.2"
palette = "0.4"
pennereq = "0.3"
rand = "0.7"
serde = "1"
Expand Down
80 changes: 40 additions & 40 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
//! palette crate. See [the palette docs](https://docs.rs/palette) for more details or see the
//! [**named**](./named/index.html) module for a set of provided color constants.
pub use self::named::*;

#[doc(inline)]
pub use palette::*;

// TODO: These named colors are actually SRGBA values but we treat them as linear RGBA. These
// named color values should be adjusted for linear RGBA.
pub mod named {
pub use self::named::*;
pub use self::tango::*;

pub mod tango {
//! A set of provided, named color constants.
//!
//! These colors come from the [Tango
//! palette](http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines) which provides
//! aesthetically reasonable defaults for colors. Each color also comes with a light and dark
//! version.
use super::{Alpha, Rgb, Rgba};
use super::{Alpha, Srgb, Srgba};

macro_rules! make_color {
($r:expr, $g:expr, $b:expr, $a:expr) => {
Alpha {
color: Rgb {
color: Srgb {
red: $r as f32 / 255.0,
green: $g as f32 / 255.0,
blue: $b as f32 / 255.0,
standard: std::marker::PhantomData,
},
alpha: $a as f32 / 255.0,
}
Expand All @@ -35,80 +35,80 @@ pub mod named {
}

/// Scarlet Red - Light - #EF2929
pub const LIGHT_RED: Rgba<f32> = make_color!(239, 41, 41);
pub const LIGHT_RED: Srgba<f32> = make_color!(239, 41, 41);
/// Scarlet Red - Regular - #CC0000
pub const RED: Rgba<f32> = make_color!(204, 0, 0);
pub const RED: Srgba<f32> = make_color!(204, 0, 0);
/// Scarlet Red - Dark - #A30000
pub const DARK_RED: Rgba<f32> = make_color!(164, 0, 0);
pub const DARK_RED: Srgba<f32> = make_color!(164, 0, 0);

/// Orange - Light - #FCAF3E
pub const LIGHT_ORANGE: Rgba<f32> = make_color!(252, 175, 62);
pub const LIGHT_ORANGE: Srgba<f32> = make_color!(252, 175, 62);
/// Orange - Regular - #F57900
pub const ORANGE: Rgba<f32> = make_color!(245, 121, 0);
pub const ORANGE: Srgba<f32> = make_color!(245, 121, 0);
/// Orange - Dark - #CE5C00
pub const DARK_ORANGE: Rgba<f32> = make_color!(206, 92, 0);
pub const DARK_ORANGE: Srgba<f32> = make_color!(206, 92, 0);

/// Butter - Light - #FCE94F
pub const LIGHT_YELLOW: Rgba<f32> = make_color!(252, 233, 79);
pub const LIGHT_YELLOW: Srgba<f32> = make_color!(252, 233, 79);
/// Butter - Regular - #EDD400
pub const YELLOW: Rgba<f32> = make_color!(237, 212, 0);
pub const YELLOW: Srgba<f32> = make_color!(237, 212, 0);
/// Butter - Dark - #C4A000
pub const DARK_YELLOW: Rgba<f32> = make_color!(196, 160, 0);
pub const DARK_YELLOW: Srgba<f32> = make_color!(196, 160, 0);

/// Chameleon - Light - #8AE234
pub const LIGHT_GREEN: Rgba<f32> = make_color!(138, 226, 52);
pub const LIGHT_GREEN: Srgba<f32> = make_color!(138, 226, 52);
/// Chameleon - Regular - #73D216
pub const GREEN: Rgba<f32> = make_color!(115, 210, 22);
pub const GREEN: Srgba<f32> = make_color!(115, 210, 22);
/// Chameleon - Dark - #4E9A06
pub const DARK_GREEN: Rgba<f32> = make_color!(78, 154, 6);
pub const DARK_GREEN: Srgba<f32> = make_color!(78, 154, 6);

/// Sky Blue - Light - #729FCF
pub const LIGHT_BLUE: Rgba<f32> = make_color!(114, 159, 207);
pub const LIGHT_BLUE: Srgba<f32> = make_color!(114, 159, 207);
/// Sky Blue - Regular - #3465A4
pub const BLUE: Rgba<f32> = make_color!(52, 101, 164);
pub const BLUE: Srgba<f32> = make_color!(52, 101, 164);
/// Sky Blue - Dark - #204A87
pub const DARK_BLUE: Rgba<f32> = make_color!(32, 74, 135);
pub const DARK_BLUE: Srgba<f32> = make_color!(32, 74, 135);

/// Plum - Light - #AD7FA8
pub const LIGHT_PURPLE: Rgba<f32> = make_color!(173, 127, 168);
pub const LIGHT_PURPLE: Srgba<f32> = make_color!(173, 127, 168);
/// Plum - Regular - #75507B
pub const PURPLE: Rgba<f32> = make_color!(117, 80, 123);
pub const PURPLE: Srgba<f32> = make_color!(117, 80, 123);
/// Plum - Dark - #5C3566
pub const DARK_PURPLE: Rgba<f32> = make_color!(92, 53, 102);
pub const DARK_PURPLE: Srgba<f32> = make_color!(92, 53, 102);

/// Chocolate - Light - #E9B96E
pub const LIGHT_BROWN: Rgba<f32> = make_color!(233, 185, 110);
pub const LIGHT_BROWN: Srgba<f32> = make_color!(233, 185, 110);
/// Chocolate - Regular - #C17D11
pub const BROWN: Rgba<f32> = make_color!(193, 125, 17);
pub const BROWN: Srgba<f32> = make_color!(193, 125, 17);
/// Chocolate - Dark - #8F5902
pub const DARK_BROWN: Rgba<f32> = make_color!(143, 89, 2);
pub const DARK_BROWN: Srgba<f32> = make_color!(143, 89, 2);

/// Straight Black.
pub const BLACK: Rgba<f32> = make_color!(0, 0, 0);
pub const BLACK: Srgba<f32> = make_color!(0, 0, 0);
/// Straight White.
pub const WHITE: Rgba<f32> = make_color!(255, 255, 255);
pub const WHITE: Srgba<f32> = make_color!(255, 255, 255);

/// Alluminium - Light
pub const LIGHT_GRAY: Rgba<f32> = make_color!(238, 238, 236);
pub const LIGHT_GRAY: Srgba<f32> = make_color!(238, 238, 236);
/// Alluminium - Regular
pub const GRAY: Rgba<f32> = make_color!(211, 215, 207);
pub const GRAY: Srgba<f32> = make_color!(211, 215, 207);
/// Alluminium - Dark
pub const DARK_GRAY: Rgba<f32> = make_color!(186, 189, 182);
pub const DARK_GRAY: Srgba<f32> = make_color!(186, 189, 182);

/// Aluminium - Light - #EEEEEC
pub const LIGHT_GREY: Rgba<f32> = make_color!(238, 238, 236);
pub const LIGHT_GREY: Srgba<f32> = make_color!(238, 238, 236);
/// Aluminium - Regular - #D3D7CF
pub const GREY: Rgba<f32> = make_color!(211, 215, 207);
pub const GREY: Srgba<f32> = make_color!(211, 215, 207);
/// Aluminium - Dark - #BABDB6
pub const DARK_GREY: Rgba<f32> = make_color!(186, 189, 182);
pub const DARK_GREY: Srgba<f32> = make_color!(186, 189, 182);

/// Charcoal - Light - #888A85
pub const LIGHT_CHARCOAL: Rgba<f32> = make_color!(136, 138, 133);
pub const LIGHT_CHARCOAL: Srgba<f32> = make_color!(136, 138, 133);
/// Charcoal - Regular - #555753
pub const CHARCOAL: Rgba<f32> = make_color!(85, 87, 83);
pub const CHARCOAL: Srgba<f32> = make_color!(85, 87, 83);
/// Charcoal - Dark - #2E3436
pub const DARK_CHARCOAL: Rgba<f32> = make_color!(46, 52, 54);
pub const DARK_CHARCOAL: Srgba<f32> = make_color!(46, 52, 54);

/// Transparent
pub const TRANSPARENT: Rgba<f32> = make_color!(0.0, 0.0, 0.0, 0.0);
pub const TRANSPARENT: Srgba<f32> = make_color!(0.0, 0.0, 0.0, 0.0);
}
20 changes: 10 additions & 10 deletions src/draw/background.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::color::{self, Rgb, Rgba};
use crate::draw::properties::{ColorScalar, IntoRgba};
use crate::color::{self, Srgb, Srgba};
use crate::draw::properties::{ColorScalar, IntoSrgba};
use crate::draw::Draw;
use crate::geom;
use crate::math::BaseFloat;
Expand Down Expand Up @@ -31,22 +31,22 @@ where
/// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
pub fn color<C>(self, color: C) -> Self
where
C: IntoRgba<ColorScalar>,
C: IntoSrgba<ColorScalar>,
{
if let Ok(mut state) = self.draw.state.try_borrow_mut() {
state.background_color = Some(color.into_rgba());
state.background_color = Some(color.into_srgba());
}
self
}

/// Specify the color via red, green and blue channels.
pub fn rgb(self, r: ColorScalar, g: ColorScalar, b: ColorScalar) -> Self {
self.color(Rgb::new(r, g, b))
self.color(Srgb::new(r, g, b))
}

/// Specify the color via red, green, blue and alpha channels.
pub fn rgba(self, r: ColorScalar, g: ColorScalar, b: ColorScalar, a: ColorScalar) -> Self {
self.color(Rgba::new(r, g, b, a))
self.color(Srgba::new(r, g, b, a))
}

/// Specify the color via hue, saturation and luminance.
Expand All @@ -60,7 +60,7 @@ where
/// this color space.
pub fn hsl(self, h: ColorScalar, s: ColorScalar, l: ColorScalar) -> Self {
let hue = h * 360.0;
self.color(color::Hsl::new(hue.into(), s, l))
self.color(color::Hsl::new(hue, s, l))
}

/// Specify the color via hue, saturation, luminance and an alpha channel.
Expand All @@ -74,7 +74,7 @@ where
/// this color space.
pub fn hsla(self, h: ColorScalar, s: ColorScalar, l: ColorScalar, a: ColorScalar) -> Self {
let hue = h * 360.0;
self.color(color::Hsla::new(hue.into(), s, l, a))
self.color(color::Hsla::new(hue, s, l, a))
}

/// Specify the color via hue, saturation and *value* (brightness).
Expand All @@ -88,7 +88,7 @@ where
/// this color space.
pub fn hsv(self, h: ColorScalar, s: ColorScalar, v: ColorScalar) -> Self {
let hue = h * 360.0;
self.color(color::Hsv::new(hue.into(), s, v))
self.color(color::Hsv::new(hue, s, v))
}

/// Specify the color via hue, saturation, *value* (brightness) and an alpha channel.
Expand All @@ -105,6 +105,6 @@ where
S: Into<color::RgbHue<S>>,
{
let hue = h * 360.0;
self.color(color::Hsva::new(hue.into(), s, v, a))
self.color(color::Hsva::new(hue, s, v, a))
}
}
4 changes: 2 additions & 2 deletions src/draw/drawing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::draw::properties::spatial::{dimension, orientation, position};
use crate::draw::properties::{
ColorScalar, IntoDrawn, IntoRgba, Primitive, SetColor, SetDimensions, SetOrientation,
ColorScalar, IntoDrawn, IntoSrgba, Primitive, SetColor, SetDimensions, SetOrientation,
SetPosition,
};
use crate::draw::{self, Draw};
Expand Down Expand Up @@ -213,7 +213,7 @@ where
/// Colors that have no alpha channel will be given an opaque alpha channel value `1.0`.
pub fn color<C>(self, color: C) -> Self
where
C: IntoRgba<ColorScalar>,
C: IntoSrgba<ColorScalar>,
{
self.map_ty(|ty| SetColor::color(ty, color))
}
Expand Down
16 changes: 12 additions & 4 deletions src/draw/mesh/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::mesh::vertex::{WithColor, WithTexCoords};
use std::marker::PhantomData;

pub type Point<S> = Point3<S>;
pub type Color = color::Rgba;
pub type Color = color::Srgba;
pub type TexCoords<S> = Point2<S>;
pub type Normal<S> = Vector3<S>;
pub type ColoredPoint<S> = WithColor<Point<S>, Color>;
Expand All @@ -27,7 +27,15 @@ pub trait IntoPoint<S> {

// IntoVertex Implementations.

const DEFAULT_VERTEX_COLOR: Color = color::WHITE;
const DEFAULT_VERTEX_COLOR: Color = color::Alpha {
color: color::rgb::Rgb {
red: 1.0,
green: 1.0,
blue: 1.0,
standard: std::marker::PhantomData,
},
alpha: 1.0,
};

impl<'a, S, T> IntoVertex<S> for &'a T
where
Expand Down Expand Up @@ -133,14 +141,14 @@ where
}
}

impl<S, V> IntoVertex<S> for geom::vertex::Rgba<V>
impl<S, V> IntoVertex<S> for geom::vertex::Srgba<V>
where
S: BaseFloat,
V: geom::Vertex<Scalar = S>,
(V, Color): IntoVertex<S>,
{
fn into_vertex(self) -> Vertex<S> {
let geom::vertex::Rgba(v, color) = self;
let geom::vertex::Srgba(v, color) = self;
(v, color).into_vertex()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/draw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where
/// The theme containing default values.
theme: Theme,
/// If `Some`, the **Draw** should first clear the frame's gl context with the given color.
background_color: Option<properties::Rgba>,
background_color: Option<properties::Srgba>,
}

/// A set of intermediary buffers for collecting geometry point data for geometry types that may
Expand Down
Loading

0 comments on commit 57170d8

Please sign in to comment.