added hex

This commit is contained in:
2024-11-08 15:48:20 +01:00
parent 5bc1c1b42d
commit 2a43bf75db
3 changed files with 40 additions and 11 deletions
+2 -8
View File
@@ -1,13 +1,7 @@
use crate::{self as osui, rsx, ui::*, val, Element}; use crate::{self as osui, rsx, ui::*, Element};
pub fn app() -> Box<dyn Element> { pub fn app() -> Box<dyn Element> {
rsx! { rsx! {
custom_component { width: val!(20), "" } button { "ok" }
}
}
pub fn custom_component<'a>() -> Box<Div<'a>> {
rsx! {
button { style.clicked_background: Color::Red, "test" }
} }
} }
+1 -1
View File
@@ -35,7 +35,7 @@ element! {
/// The `Button` element can be clicked, triggering an `on_click` function. Its appearance changes /// The `Button` element can be clicked, triggering an `on_click` function. Its appearance changes
/// based on its interaction state, such as being "clicked". /// based on its interaction state, such as being "clicked".
Button { Button {
/// A callback function executed when the button is clicked. /// A callback function executed when the button is clicked. use `arc!` to use function
pub on_click: Arc<Mutex<dyn FnMut(&mut Button)>>, pub on_click: Arc<Mutex<dyn FnMut(&mut Button)>>,
} }
+37 -2
View File
@@ -45,7 +45,9 @@ impl Font {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Color { pub enum Color {
None, None,
/// Red, Green, blue
Rgb(u8, u8, u8), // RGB color representation. Rgb(u8, u8, u8), // RGB color representation.
Hex(String),
Black, Black,
Red, Red,
Green, Green,
@@ -61,7 +63,11 @@ impl Color {
pub fn ansi(&self) -> String { pub fn ansi(&self) -> String {
String::from(match self { String::from(match self {
Color::None => "", Color::None => "",
Self::Rgb(r, g, b) => { Color::Rgb(r, g, b) => {
return format!("\x1b[38;2;{r};{g};{b}m");
}
Color::Hex(hex) => {
let (r, g, b) = hex_to_rgb(hex);
return format!("\x1b[38;2;{r};{g};{b}m"); return format!("\x1b[38;2;{r};{g};{b}m");
} }
Color::Black => "\x1b[30m", Color::Black => "\x1b[30m",
@@ -79,7 +85,11 @@ impl Color {
pub fn ansi_bg(&self) -> String { pub fn ansi_bg(&self) -> String {
String::from(match self { String::from(match self {
Color::None => "", Color::None => "",
Self::Rgb(r, g, b) => { Color::Rgb(r, g, b) => {
return format!("\x1b[48;2;{r};{g};{b}m");
}
Color::Hex(hex) => {
let (r, g, b) = hex_to_rgb(hex);
return format!("\x1b[48;2;{r};{g};{b}m"); return format!("\x1b[48;2;{r};{g};{b}m");
} }
Color::Black => "\x1b[40m", Color::Black => "\x1b[40m",
@@ -115,3 +125,28 @@ impl Default for Font {
Font::None Font::None
} }
} }
fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
let hex = hex.trim_start_matches('#');
// Expand 3-character hex codes to 6 characters
let hex = if hex.len() == 3 {
format!(
"{}{}{}{}{}{}",
&hex[0..1], &hex[0..1],
&hex[1..2], &hex[1..2],
&hex[2..3], &hex[2..3]
)
} else if hex.len() == 6 {
hex.to_string()
} else {
return (255, 255, 255); // Return white for invalid hex length
};
// Parse each pair of characters as u8
let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(255);
let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(255);
let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(255);
(r, g, b)
}