Skip to content

Commit

Permalink
Merge #97
Browse files Browse the repository at this point in the history
97: Add hexadecimal formatting to Alpha, Luma and Rgb r=Ogeon a=Ogeon

Closes #80.

Co-authored-by: Erik Hedvall <[email protected]>
  • Loading branch information
bors[bot] and Ogeon committed Apr 30, 2018
2 parents c684063 + 84c1286 commit a41f194
Show file tree
Hide file tree
Showing 3 changed files with 340 additions and 7 deletions.
140 changes: 137 additions & 3 deletions palette/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
use std::fmt;

use num_traits::Float;

Expand Down Expand Up @@ -312,24 +313,157 @@ impl<C, T: Component> From<C> for Alpha<C, T> {
}
}

impl<C, T> fmt::LowerHex for Alpha<C, T>
where
T: fmt::LowerHex,
C: fmt::LowerHex,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let size = f.width().unwrap_or(::std::mem::size_of::<T>() * 2);
write!(
f,
"{:0width$x}{:0width$x}",
self.color,
self.alpha,
width = size
)
}
}

impl<C, T> fmt::UpperHex for Alpha<C, T>
where
T: fmt::UpperHex,
C: fmt::UpperHex,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let size = f.width().unwrap_or(::std::mem::size_of::<T>() * 2);
write!(
f,
"{:0width$X}{:0width$X}",
self.color,
self.alpha,
width = size
)
}
}

#[cfg(test)]
#[cfg(feature = "serde")]
mod test {
use rgb::Rgba;
use encoding::Srgb;

#[test]
fn lower_hex() {
assert_eq!(
format!("{:x}", Rgba::<Srgb, u8>::new(171, 193, 35, 161)),
"abc123a1"
);
}

#[test]
fn lower_hex_small_numbers() {
assert_eq!(
format!("{:x}", Rgba::<Srgb, u8>::new(1, 2, 3, 4)),
"01020304"
);
assert_eq!(
format!("{:x}", Rgba::<Srgb, u16>::new(1, 2, 3, 4)),
"0001000200030004"
);
assert_eq!(
format!("{:x}", Rgba::<Srgb, u32>::new(1, 2, 3, 4)),
"00000001000000020000000300000004"
);
assert_eq!(
format!("{:x}", Rgba::<Srgb, u64>::new(1, 2, 3, 4)),
"0000000000000001000000000000000200000000000000030000000000000004"
);
}

#[test]
fn lower_hex_custom_width() {
assert_eq!(
format!("{:03x}", Rgba::<Srgb, u8>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03x}", Rgba::<Srgb, u16>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03x}", Rgba::<Srgb, u32>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03x}", Rgba::<Srgb, u64>::new(1, 2, 3, 4)),
"001002003004"
);
}

#[test]
fn upper_hex() {
assert_eq!(
format!("{:X}", Rgba::<Srgb, u8>::new(171, 193, 35, 161)),
"ABC123A1"
);
}

#[test]
fn upper_hex_small_numbers() {
assert_eq!(
format!("{:X}", Rgba::<Srgb, u8>::new(1, 2, 3, 4)),
"01020304"
);
assert_eq!(
format!("{:X}", Rgba::<Srgb, u16>::new(1, 2, 3, 4)),
"0001000200030004"
);
assert_eq!(
format!("{:X}", Rgba::<Srgb, u32>::new(1, 2, 3, 4)),
"00000001000000020000000300000004"
);
assert_eq!(
format!("{:X}", Rgba::<Srgb, u64>::new(1, 2, 3, 4)),
"0000000000000001000000000000000200000000000000030000000000000004"
);
}

#[test]
fn upper_hex_custom_width() {
assert_eq!(
format!("{:03X}", Rgba::<Srgb, u8>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03X}", Rgba::<Srgb, u16>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03X}", Rgba::<Srgb, u32>::new(1, 2, 3, 4)),
"001002003004"
);
assert_eq!(
format!("{:03X}", Rgba::<Srgb, u64>::new(1, 2, 3, 4)),
"001002003004"
);
}

#[cfg(feature = "serde")]
#[test]
fn serialize() {
let serialized = ::serde_json::to_string(&Rgba::<Srgb>::new(0.3, 0.8, 0.1, 0.5)).unwrap();

assert_eq!(serialized, r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#);
assert_eq!(
serialized,
r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#
);
}

#[cfg(feature = "serde")]
#[test]
fn deserialize() {
let deserialized: Rgba<Srgb> = ::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#).unwrap();
let deserialized: Rgba<Srgb> =
::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#).unwrap();

assert_eq!(deserialized, Rgba::<Srgb>::new(0.3, 0.8, 0.1, 0.5));
}
Expand Down
77 changes: 74 additions & 3 deletions palette/src/luma/luma.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::ops::{Add, Div, Mul, Sub};
use std::marker::PhantomData;
use std::fmt;

use approx::ApproxEq;

use num_traits::Float;

use std::ops::{Add, Div, Mul, Sub};
use std::marker::PhantomData;

use {Alpha, Xyz, Yxy};
use {Blend, Component, ComponentWise, FromColor, IntoColor, Limited, Mix, Pixel, Shade};
use luma::LumaStandard;
Expand Down Expand Up @@ -533,6 +534,28 @@ where
}
}

impl<S, T> fmt::LowerHex for Luma<S, T>
where
T: Component + fmt::LowerHex,
S: LumaStandard,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let size = f.width().unwrap_or(::std::mem::size_of::<T>() * 2);
write!(f, "{:0width$x}", self.luma, width = size)
}
}

impl<S, T> fmt::UpperHex for Luma<S, T>
where
T: Component + fmt::UpperHex,
S: LumaStandard,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let size = f.width().unwrap_or(::std::mem::size_of::<T>() * 2);
write!(f, "{:0width$X}", self.luma, width = size)
}
}

#[cfg(test)]
mod test {
use Luma;
Expand All @@ -552,6 +575,54 @@ mod test {

raw_pixel_conversion_tests!(Luma<Srgb>: luma);

#[test]
fn lower_hex() {
assert_eq!(format!("{:x}", Luma::<Srgb, u8>::new(161)), "a1");
}

#[test]
fn lower_hex_small_numbers() {
assert_eq!(format!("{:x}", Luma::<Srgb, u8>::new(1)), "01");
assert_eq!(format!("{:x}", Luma::<Srgb, u16>::new(1)), "0001");
assert_eq!(format!("{:x}", Luma::<Srgb, u32>::new(1)), "00000001");
assert_eq!(
format!("{:x}", Luma::<Srgb, u64>::new(1)),
"0000000000000001"
);
}

#[test]
fn lower_hex_custom_width() {
assert_eq!(format!("{:03x}", Luma::<Srgb, u8>::new(1)), "001");
assert_eq!(format!("{:03x}", Luma::<Srgb, u16>::new(1)), "001");
assert_eq!(format!("{:03x}", Luma::<Srgb, u32>::new(1)), "001");
assert_eq!(format!("{:03x}", Luma::<Srgb, u64>::new(1)), "001");
}

#[test]
fn upper_hex() {
assert_eq!(format!("{:X}", Luma::<Srgb, u8>::new(161)), "A1");
}

#[test]
fn upper_hex_small_numbers() {
assert_eq!(format!("{:X}", Luma::<Srgb, u8>::new(1)), "01");
assert_eq!(format!("{:X}", Luma::<Srgb, u16>::new(1)), "0001");
assert_eq!(format!("{:X}", Luma::<Srgb, u32>::new(1)), "00000001");
assert_eq!(
format!("{:X}", Luma::<Srgb, u64>::new(1)),
"0000000000000001"
);
}

#[test]
fn upper_hex_custom_width() {
assert_eq!(format!("{:03X}", Luma::<Srgb, u8>::new(1)), "001");
assert_eq!(format!("{:03X}", Luma::<Srgb, u16>::new(1)), "001");
assert_eq!(format!("{:03X}", Luma::<Srgb, u32>::new(1)), "001");
assert_eq!(format!("{:03X}", Luma::<Srgb, u64>::new(1)), "001");
}

#[cfg(feature = "serde")]
#[test]
fn serialize() {
Expand Down
Loading

0 comments on commit a41f194

Please sign in to comment.