Skip to content

Commit

Permalink
[#46] MR
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed Nov 1, 2023
1 parent afaa145 commit d053a3a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 61 deletions.
2 changes: 1 addition & 1 deletion rpg_tools_core/src/model/equipment/appearance/eyewear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct LensStyle {
pub lens_transparency: Transparency,
}

/// The frame of the [`eyewear's`](Eyewear).
/// The frame of the [`eyewear`](Eyewear).
#[derive(Convert, ui, Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum FrameType {
Horn,
Expand Down
58 changes: 0 additions & 58 deletions rpg_tools_rendering/src/renderer/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,6 @@ impl WebColor {
pub fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> WebColor {
WebColor::RBGA { r, g, b, a }
}

/// Converts a string to a color, if possible:
///
/// ```
///# use rpg_tools_rendering::renderer::color::WebColor;
/// assert_eq!(WebColor::convert("#FFA500").unwrap(), WebColor::from_rgb(255, 165, 0));
/// assert_eq!(WebColor::convert("#FFA50040").unwrap(), WebColor::from_rgba(255, 165, 0, 64));
/// ```
pub fn convert(hex_code: &str) -> Option<WebColor> {
if !hex_code.starts_with('#') {
return None;
} else if hex_code.len() != 7 && hex_code.len() != 9 {
return None;
}

let r: u8 = u8::from_str_radix(&hex_code[1..3], 16).ok()?;
let g: u8 = u8::from_str_radix(&hex_code[3..5], 16).ok()?;
let b: u8 = u8::from_str_radix(&hex_code[5..7], 16).ok()?;

if hex_code.len() == 7 {
return Some(WebColor::from_rgb(r, g, b));
}

let a: u8 = u8::from_str_radix(&hex_code[7..9], 16).ok()?;

Some(WebColor::from_rgba(r, g, b, a))
}
}

impl Display for WebColor {
Expand All @@ -90,34 +63,3 @@ impl Display for WebColor {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_empty_string() {
assert!(WebColor::convert("").is_none());
}

#[test]
fn test_from_string_invalid_start() {
assert!(WebColor::convert("FFA500").is_none());
}

#[test]
fn test_from_string_wrong_length() {
assert!(WebColor::convert("#").is_none());
assert!(WebColor::convert("#FF").is_none());
assert!(WebColor::convert("#FFA5").is_none());
assert!(WebColor::convert("#FFA50").is_none());
assert!(WebColor::convert("#FFA500FFA5").is_none());
}

#[test]
fn test_from_string_ignore_case() {
let orange = WebColor::from_rgb(255, 165, 0);
assert_eq!(WebColor::convert("#FFA500").unwrap(), orange);
assert_eq!(WebColor::convert("#ffa500").unwrap(), orange);
}
}
4 changes: 2 additions & 2 deletions rpg_tools_rendering/src/renderer/svg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ fn to_color(color: &WebColor, text: &str) -> String {
match color {
WebColor::Transparent(name, transparency) => {
let opacity = 1.0 - (*transparency as f32 / 255.0);
return format!(
format!(
"{0}:{1};{0}-opacity:{2}",
text,
name.to_lowercase(),
opacity
);
)
}
_ => format!("{}:{}", text, color.to_string().to_lowercase()),
}
Expand Down

0 comments on commit d053a3a

Please sign in to comment.