use alloc::string::String;
pub const WS_VISIBLE: u32 = 0x1000_0000;
pub const WS_CHILD: u32 = 0x4000_0000;
pub const WS_POPUP: u32 = 0x8000_0000;
pub const WS_MINIMIZE: u32 = 0x2000_0000;
pub const WS_MAXIMIZE: u32 = 0x0100_0000;
pub const WS_DISABLED: u32 = 0x0800_0000;
pub const WS_CAPTION: u32 = 0x00C0_0000;
pub const WS_BORDER: u32 = 0x0000_800000;
pub const WS_SYSMENU: u32 = 0x0008_0000;
pub const WS_THICKFRAME: u32 = 0x0004_0000;
pub const WS_GROUP: u32 = 0x0002_0000;
pub const WS_TABSTOP: u32 = 0x0001_0000;
pub const WS_MINIMIZEBOX: u32 = 0x0002_0000;
pub const WS_MAXIMIZEBOX: u32 = 0x0001_0000;
pub const WS_VSCROLL: u32 = 0x0020_0000;
pub const WS_HSCROLL: u32 = 0x0010_0000;
pub const WS_EX_TOPMOST: u32 = 0x0000_0008;
pub const WS_EX_TRANSPARENT: u32 = 0x0000_0020;
pub const WS_EX_TOOLWINDOW: u32 = 0x0000_0080;
pub const WS_EX_WINDOWEDGE: u32 = 0x0000_0100;
pub const WS_EX_CLIENTEDGE: u32 = 0x0000_0200;
pub const WS_EX_CONTEXTHELP: u32 = 0x0000_0400;
pub const WS_EX_RTLREADING: u32 = 0x0000_2000;
pub const WS_EX_CONTROLPARENT: u32 = 0x0001_0000;
pub const WS_EX_APPWINDOW: u32 = 0x0004_0000;
pub const WS_EX_LAYERED: u32 = 0x0008_0000;
pub const WS_EX_NOACTIVATE: u32 = 0x0800_0000;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StyleChange {
Replace(u32),
Add(u32),
Remove(u32),
Toggle(u32),
}
impl StyleChange {
pub fn the_style_of(&self, current: u32) -> u32 {
match self {
StyleChange::Replace(style) => *style,
StyleChange::Add(bits) => current | bits,
StyleChange::Remove(bits) => current & !bits,
StyleChange::Toggle(bits) => current ^ bits,
}
}
}
pub fn the_style_change_of_a_text(text: &str) -> Option<StyleChange> {
if text.is_empty() {
return None;
}
let (change, digits) = match text.as_bytes()[0] {
b'+' => (StyleChange::Add as fn(u32) -> StyleChange, &text[1..]),
b'-' => (StyleChange::Remove as fn(u32) -> StyleChange, &text[1..]),
b'^' => (StyleChange::Toggle as fn(u32) -> StyleChange, &text[1..]),
_ => (StyleChange::Replace as fn(u32) -> StyleChange, text),
};
Some(change(the_unsigned_of_a_text(digits)))
}
pub fn the_unsigned_of_a_text(text: &str) -> u32 {
let text = text.trim();
if text.is_empty() {
return 0;
}
if let Some(digits) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
if !digits.is_empty() && digits.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return u32::from_str_radix(digits, 16).unwrap_or(u32::MAX);
}
return 0;
}
let digits: String = text
.chars()
.take_while(|digit| digit.is_ascii_digit())
.collect();
digits.parse::<u32>().unwrap_or(0)
}
pub fn the_colour_of_a_text(text: &str) -> Option<u32> {
let text = text.trim();
if text.is_empty() {
return Some(COLOUR_NONE);
}
if let Some(colour) = the_html_colour(text) {
return Some(colour);
}
let digits = text
.strip_prefix("0x")
.or_else(|| text.strip_prefix("0X"))
.unwrap_or(text);
if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return None;
}
u32::from_str_radix(digits, 16)
.ok()
.map(|colour| colour & 0x0000_FFFFFF)
}
pub const COLOUR_NONE: u32 = 0x0200_0000;
pub fn the_html_colour(name: &str) -> Option<u32> {
let colour = match name.to_ascii_lowercase().as_str() {
"black" => 0x000000,
"silver" => 0xC0C0C0,
"gray" => 0x808080,
"white" => 0xFFFFFF,
"maroon" => 0x800000,
"red" => 0xFF0000,
"purple" => 0x800080,
"fuchsia" => 0xFF00FF,
"green" => 0x008000,
"lime" => 0x00FF00,
"olive" => 0x808000,
"yellow" => 0xFFFF00,
"navy" => 0x000080,
"blue" => 0x0000FF,
"teal" => 0x008080,
"aqua" => 0x00FFFF,
_ => return None,
};
Some(colour)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_empty_text_is_no_change() {
assert_eq!(the_style_change_of_a_text(""), None);
}
#[test]
fn the_text_without_a_prefix_replaces_the_style() {
assert_eq!(
the_style_change_of_a_text("0x10"),
Some(StyleChange::Replace(0x10))
);
assert_eq!(
the_style_change_of_a_text("16"),
Some(StyleChange::Replace(16))
);
}
#[test]
fn the_prefix_says_what_to_do_with_the_style_that_is_there() {
assert_eq!(
the_style_change_of_a_text("+0x10"),
Some(StyleChange::Add(0x10))
);
assert_eq!(
the_style_change_of_a_text("-0x10"),
Some(StyleChange::Remove(0x10))
);
assert_eq!(
the_style_change_of_a_text("^0x10"),
Some(StyleChange::Toggle(0x10))
);
assert_eq!(
StyleChange::Add(0x10).the_style_of(0x20),
0x30,
"the bits are added"
);
assert_eq!(
StyleChange::Remove(0x10).the_style_of(0x30),
0x20,
"the bits are taken away"
);
assert_eq!(
StyleChange::Toggle(0x10).the_style_of(0x30),
0x20,
"the bits are turned around"
);
assert_eq!(
StyleChange::Replace(0x10).the_style_of(0x20),
0x10,
"the style is replaced"
);
}
#[test]
fn a_text_that_holds_no_number_is_zero() {
assert_eq!(the_unsigned_of_a_text("zzz"), 0);
assert_eq!(the_unsigned_of_a_text(""), 0);
assert_eq!(the_unsigned_of_a_text("0x1F"), 31);
assert_eq!(the_unsigned_of_a_text("16"), 16);
assert_eq!(the_unsigned_of_a_text("16px"), 16);
}
#[test]
fn the_colours_of_html_are_the_seventeen_of_the_v2() {
assert_eq!(the_colour_of_a_text("Red"), Some(0x00FF_0000));
assert_eq!(the_colour_of_a_text("blue"), Some(0x0000_00FF));
assert_eq!(the_colour_of_a_text("White"), Some(0x0000_FFFFFF));
assert_eq!(the_colour_of_a_text("Green"), Some(0x0000_8000));
}
#[test]
fn a_colour_is_read_as_hexadecimal() {
assert_eq!(the_colour_of_a_text("FF00FF"), Some(0x00FF_00FF));
assert_eq!(the_colour_of_a_text("0xFF00FF"), Some(0x00FF_00FF));
assert_eq!(the_colour_of_a_text("65536"), Some(0x0006_5536));
assert_eq!(the_colour_of_a_text("zzz"), None);
assert_eq!(the_colour_of_a_text(""), Some(COLOUR_NONE));
}
}