color_conv/lib.rs
1#![deny(
2 missing_docs,
3 missing_doc_code_examples,
4 trivial_casts,
5 trivial_numeric_casts
6)]
7
8//!
9//! `color_conv` is a helper library for easily and programmatically converting
10//! between the `RGB`, `CMYK`, `HSL`, and `hex` color formats.
11//!
12//! ```toml
13//! [dependencies]
14//! color_conv = "0.2.1"
15//! ```
16//!
17//! # Example
18//!
19//! ```
20//! use color_conv::Color;
21//! use color_conv::Cmyk;
22//! use color_conv::Rgb;
23//!
24//! let cyan = Cmyk::new_unchecked(100, 0, 0, 0);
25//! let cyan_rgb = cyan.to_rgb();
26//!
27//! assert_eq!(Rgb::new(0, 255, 255), cyan_rgb);
28//! ```
29//!
30
31/// CMYK-specific structures
32pub mod cmyk;
33/// HSL-specific strucures
34pub mod hsl;
35/// RGB-specific strucures
36pub mod rgb;
37
38pub use self::{cmyk::Cmyk, hsl::Hsl, rgb::Rgb};
39use thiserror::Error as ThisError;
40
41#[derive(ThisError, Debug)]
42///
43/// Crate-wide Error type.
44///
45pub enum Error {
46 ///
47 /// Occurs when a parameter representing a percentage value is greater than
48 /// 100. This error can be thrown by [`Cmyk::new`](crate::Cmyk::new) or
49 /// [`Hsl::new`](crate::Hsl::new), both of which perform this check.
50 ///
51 #[error("Percentage overflow: value is larger than 100!")]
52 PercentageOverflow,
53 ///
54 /// Occurs when a parameter representing a degree value is greater than 360.
55 /// 100. This error can be thrown by [`Hsl::new`](crate::Hsl::new), which
56 /// performs this check.
57 ///
58 #[error("Degree overflow: value is larger than 360!")]
59 DegreeOverflow,
60}
61
62///
63/// Unifying `Color` trait which encompasses each of the structs provided by
64/// this crate.
65///
66pub trait Color {
67 ///
68 /// Convert to [`Rgb`]
69 ///
70 /// # Examples
71 ///
72 /// ```
73 /// use color_conv::Color;
74 /// use color_conv::Cmyk;
75 /// use color_conv::Rgb;
76 ///
77 /// let cyan = Cmyk::new_unchecked(100, 0, 0, 0);
78 /// let cyan_rgb = cyan.to_rgb();
79 ///
80 /// assert_eq!(Rgb::new(0, 255, 255), cyan_rgb);
81 /// # Ok::<(), color_conv::Error>(())
82 /// ```
83 ///
84 fn to_rgb(self) -> Rgb;
85
86 ///
87 /// Convert to [`Cmyk`] with the possibility of failing if any of the
88 /// percentage values are above 100
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use color_conv::Color;
94 /// use color_conv::Rgb;
95 /// use color_conv::Cmyk;
96 ///
97 /// let cyan = Rgb::new(0, 255, 255);
98 /// let cyan_cmyk = cyan.to_cmyk();
99 ///
100 /// assert_eq!(cyan_cmyk, Cmyk::new_unchecked(100, 0, 0, 0));
101 /// ```
102 ///
103 fn to_cmyk(self) -> Cmyk;
104
105 ///
106 /// Convert to [`Hsl`] with the possibility of failing if any of the
107 /// percentage values are above 100 or degree values are above 360
108 ///
109 /// # Examples
110 ///
111 /// ```
112 /// use color_conv::Color;
113 /// use color_conv::Hsl;
114 /// use color_conv::Cmyk;
115 ///
116 /// let cyan = Hsl::new_unchecked(180, 100, 50);
117 /// let cyan_cmyk = cyan.to_cmyk();
118 ///
119 /// assert_eq!(cyan_cmyk, Cmyk::new_unchecked(100, 0, 0, 0));
120 /// ```
121 ///
122 fn to_hsl(self) -> Hsl;
123
124 ///
125 /// Convert to a [`String`] containing the hex code of the color prefixed
126 /// with a hashtag (`#`)
127 ///
128 /// # Examples
129 ///
130 /// ```
131 /// use color_conv::Color;
132 /// use color_conv::Rgb;
133 ///
134 /// let cyan = Rgb::new(0, 255, 255);
135 /// let cyan_hex = cyan.to_hex_string();
136 ///
137 /// assert_eq!(cyan_hex, String::from("#00ffff"));
138 /// ```
139 ///
140 fn to_hex_string(self) -> String;
141}