11#![ allow( dead_code) ]
22
3+ // Special note from Gavin: if you call it "colour", you are WRONG,
4+ // which is why we will have ZERO cross-compatibility with that name.
5+ // you will be FORCED to type "color" until you realize that it is superior.
6+
37// TODO docs
48
59use std:: fmt:: { self } ;
@@ -128,6 +132,7 @@ impl Float for f64 {
128132 }
129133}
130134
135+ /// Decode an 8 bit sRGB value into a linear float.
131136#[ inline]
132137fn decode_srgb < T : Float > ( srgb_u8 : u8 ) -> T {
133138 let srgb = T :: from_f64 ( ( srgb_u8 as f64 ) / 255.0 ) ;
@@ -146,6 +151,7 @@ fn decode_srgb<T: Float>(srgb_u8: u8) -> T {
146151 }
147152}
148153
154+ /// Encode a linear float into an 8 bit sRGB value.
149155#[ inline]
150156fn encode_srgb < T : Float > ( lin : T ) -> u8 {
151157 let l = lin. clamp01 ( ) ;
@@ -167,10 +173,6 @@ fn encode_srgb<T: Float>(lin: T) -> u8 {
167173 y as u8
168174}
169175
170- // -------------------------------- //
171- // ---------- color base ---------- //
172- // -------------------------------- //
173-
174176// stores sRGB under the hood, with lots of conversion funcs
175177#[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
176178pub struct Color {
@@ -710,10 +712,6 @@ impl fmt::Display for Color {
710712 }
711713}
712714
713- // ------------------------------------ //
714- // ---------- string parsing ---------- //
715- // ------------------------------------ //
716-
717715#[ derive( Debug , Clone , PartialEq , Eq ) ]
718716pub enum ColorParseError {
719717 Empty ,
@@ -737,10 +735,13 @@ impl fmt::Display for ColorParseError {
737735}
738736impl std:: error:: Error for ColorParseError { }
739737
740- // -------------------------------------------- //
741- // ---------- string parsing helpers ---------- //
742- // -------------------------------------------- //
743-
738+ /// Parse a hex color from a string.
739+ ///
740+ /// The allowed formats are:
741+ /// * #RGB
742+ /// * #RGBA
743+ /// * #RRGGBB
744+ /// * #RRGGBBAA
744745fn parse_hex ( hex : & str ) -> Result < Color , ColorParseError > {
745746 use ColorParseError :: * ;
746747
@@ -810,6 +811,13 @@ fn parse_hex(hex: &str) -> Result<Color, ColorParseError> {
810811 Ok ( Color :: from_rgba ( [ r, g, b, a] ) )
811812}
812813
814+ /// Parse a CSS rgb function.
815+ ///
816+ /// The allowed styles are:
817+ /// rgb(r,g,b)
818+ /// rgb(r g b) (TODO: needs implementation)
819+ /// rgb(r% g% b%) (TODO: needs implementation)
820+ /// rgb(r g b / a) (TODO: needs implementation)
813821fn parse_css_rgb ( args : & str ) -> Result < Color , ColorParseError > {
814822 use ColorParseError :: * ;
815823
@@ -837,6 +845,13 @@ fn parse_css_rgb(args: &str) -> Result<Color, ColorParseError> {
837845 Ok ( Color :: from_rgb ( [ r, g, b] ) )
838846}
839847
848+ /// Parse a CSS rgba function.
849+ ///
850+ /// The allowed styles are:
851+ /// rgba(r,g,b,a)
852+ /// rgba(r g b a) (TODO: needs implementation)
853+ /// rgba(r% g% b% a%) (TODO: needs implementation)
854+ /// rgba(r g b / a) (TODO: needs implementation)
840855fn parse_css_rgba ( args : & str ) -> Result < Color , ColorParseError > {
841856 use ColorParseError :: * ;
842857
@@ -875,10 +890,6 @@ fn parse_css_rgba(args: &str) -> Result<Color, ColorParseError> {
875890 Ok ( Color :: from_rgba ( [ r, g, b, a] ) )
876891}
877892
878- // ---------------------------------------- //
879- // ---------- THE BIG BOY PARSER ---------- //
880- // ---------------------------------------- //
881-
882893pub fn parse_color ( mut s : & str ) -> Result < Color , ColorParseError > {
883894 use ColorParseError :: * ;
884895
0 commit comments