feat: hex convertion implemented
This commit is contained in:
@@ -5,6 +5,8 @@ mod test;
|
|||||||
pub mod hsl;
|
pub mod hsl;
|
||||||
pub mod rgb;
|
pub mod rgb;
|
||||||
|
|
||||||
|
use std::fmt::{UpperHex, write};
|
||||||
|
|
||||||
use crate::core::ranged::RangedInt;
|
use crate::core::ranged::RangedInt;
|
||||||
|
|
||||||
pub type ColorIntensity = RangedInt<0, 255>;
|
pub type ColorIntensity = RangedInt<0, 255>;
|
||||||
@@ -24,6 +26,12 @@ impl Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UpperHex for Color {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{:X}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PartialEq for Color {
|
impl PartialEq for Color {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.0 == other.0
|
self.0 == other.0
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::UpperHex;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
color::{ColorIntensity, HSL, RGB},
|
color::{ColorIntensity, HSL, RGB},
|
||||||
core::ranged::BaseNumber,
|
core::ranged::BaseNumber,
|
||||||
@@ -23,6 +25,12 @@ impl PartialEq for RGB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UpperHex for RGB {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{:0>2X}{:0>2X}{:0>2X}", self.0, self.1, self.2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 {
|
fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 {
|
||||||
if t < 0.0 {
|
if t < 0.0 {
|
||||||
t += 1.0;
|
t += 1.0;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
|
use std::fmt::format;
|
||||||
|
|
||||||
use crate::color::{Color, HSL, RGB};
|
use crate::color::{Color, HSL, RGB};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -89,6 +91,31 @@ pub mod tests {
|
|||||||
assert_eq!(purple_rgb, RGB::from(purple_hsl));
|
assert_eq!(purple_rgb, RGB::from(purple_hsl));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_basic_hex_convertion() {
|
||||||
|
let red_color = Color::from(RGB::new(255, 0, 0));
|
||||||
|
let green_color = Color::from(RGB::new(0, 255, 0));
|
||||||
|
let blue_color = Color::from(RGB::new(0, 0, 255));
|
||||||
|
|
||||||
|
assert_eq!(format!("{:X}", red_color), "FF0000");
|
||||||
|
assert_eq!(format!("{:X}", green_color), "00FF00");
|
||||||
|
assert_eq!(format!("{:X}", blue_color), "0000FF");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_complex_hex_convertion() {
|
||||||
|
let color = Color::from(RGB::new(255, 183, 3));
|
||||||
|
assert_eq!(format!("{:X}", color), "FFB703");
|
||||||
|
let color = Color::from(RGB::new(88, 129, 87));
|
||||||
|
assert_eq!(format!("{:X}", color), "588157");
|
||||||
|
let color = Color::from(RGB::new(251, 133, 0));
|
||||||
|
assert_eq!(format!("{:X}", color), "FB8500");
|
||||||
|
let color = Color::from(RGB::new(131, 56, 236));
|
||||||
|
assert_eq!(format!("{:X}", color), "8338EC");
|
||||||
|
let color = Color::from(RGB::new(157, 129, 137));
|
||||||
|
assert_eq!(format!("{:X}", color), "9D8189");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_color_initialization() {
|
fn test_color_initialization() {
|
||||||
let red_hsl = Color::from(HSL::new(0, 100, 50));
|
let red_hsl = Color::from(HSL::new(0, 100, 50));
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::{Display, UpperHex};
|
||||||
|
|
||||||
mod foreign_operations;
|
mod foreign_operations;
|
||||||
mod self_operations;
|
mod self_operations;
|
||||||
|
|
||||||
@@ -18,3 +20,15 @@ impl<const LOW: BaseNumber, const HIGH: BaseNumber> RangedInt<{ LOW }, { HIGH }>
|
|||||||
self.0 as f32
|
self.0 as f32
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const LOW: BaseNumber, const HIGH: BaseNumber> Display for RangedInt<{ LOW }, { HIGH }> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const LOW: BaseNumber, const HIGH: BaseNumber> UpperHex for RangedInt<{ LOW }, { HIGH }> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
UpperHex::fmt(&self.0, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,6 @@ fn main() {
|
|||||||
|
|
||||||
let hsl_color = Color::from(HSL::new(0, 100, 50));
|
let hsl_color = Color::from(HSL::new(0, 100, 50));
|
||||||
// let rgb_color = Color::from(HSL::new(193, 67, 28));
|
// let rgb_color = Color::from(HSL::new(193, 67, 28));
|
||||||
println!("HSL Color: {}", hsl_color.format());
|
println!("RGB Color: {:X}", hsl_color);
|
||||||
// println!("RGB Color: {}", rgb_color.format());
|
// println!("RGB Color: {}", rgb_color.format());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user