Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/graphics.rs')
-rw-r--r--helix-view/src/graphics.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs
index a26823b9..fcc037ed 100644
--- a/helix-view/src/graphics.rs
+++ b/helix-view/src/graphics.rs
@@ -263,6 +263,31 @@ pub enum Color {
Indexed(u8),
}
+impl Color {
+ /// Creates a `Color` from a hex string
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// use helix_view::theme::Color;
+ ///
+ /// let color1 = Color::from_hex("#c0ffee").unwrap();
+ /// let color2 = Color::Rgb(192, 255, 238);
+ ///
+ /// assert_eq!(color1, color2);
+ /// ```
+ pub fn from_hex(hex: &str) -> Option<Self> {
+ if !(hex.starts_with('#') && hex.len() == 7) {
+ return None;
+ }
+ match [1..=2, 3..=4, 5..=6].map(|i| hex.get(i).and_then(|c| u8::from_str_radix(c, 16).ok()))
+ {
+ [Some(r), Some(g), Some(b)] => Some(Self::Rgb(r, g, b)),
+ _ => None,
+ }
+ }
+}
+
#[cfg(feature = "term")]
impl From<Color> for crossterm::style::Color {
fn from(color: Color) -> Self {