mirror of
https://github.com/mistricky/codesnap.nvim.git
synced 2024-12-26 19:36:29 +00:00
[Feat] convert rgb hex color to rgba hex color
This commit is contained in:
parent
8a05bddadb
commit
14d2445c32
2 changed files with 27 additions and 18 deletions
|
@ -1,27 +1,44 @@
|
|||
use std::i64;
|
||||
use tiny_skia::Color;
|
||||
|
||||
const HEX_COLOR_LENGTH: usize = 7;
|
||||
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;
|
||||
|
||||
pub struct RgbaColor {
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
pub fn is_valid_hex_color(color: &str) -> bool {
|
||||
(color.len() == HEX_COLOR_LENGTH || color.len() == HEX_COLOR_WITH_ALPHA_LENGTH)
|
||||
&& color.starts_with("#")
|
||||
}
|
||||
|
||||
fn parse_color_to_rgba_hex(hex: &str) -> String {
|
||||
if !is_valid_hex_color(&hex) || hex.len() == HEX_COLOR_WITH_ALPHA_LENGTH {
|
||||
hex.to_string()
|
||||
} else {
|
||||
format!("{}ff", hex)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<RgbaColor> for String {
|
||||
fn into(self) -> RgbaColor {
|
||||
let hex_color = &self.to_lowercase()[1..self.len()];
|
||||
let rgba_hex_color = parse_color_to_rgba_hex(&self);
|
||||
// Remove the '#' symbol
|
||||
let hex_color = &rgba_hex_color.to_lowercase()[1..rgba_hex_color.len()];
|
||||
let chars = hex_color.chars().collect::<Vec<char>>();
|
||||
let splits = &chars
|
||||
.chunks(2)
|
||||
.map(|chunk| i64::from_str_radix(&chunk.iter().collect::<String>(), 16).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let alpha: i64;
|
||||
match splits.get(3) {
|
||||
Some(x) => alpha = *x,
|
||||
None => alpha = 255,
|
||||
}
|
||||
|
||||
RgbaColor {
|
||||
color: Color::from_rgba8(splits[0] as u8, splits[1] as u8, splits[2] as u8, alpha as u8),
|
||||
color: Color::from_rgba8(
|
||||
splits[0] as u8,
|
||||
splits[1] as u8,
|
||||
splits[2] as u8,
|
||||
splits[3] as u8,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use tiny_skia::{
|
|||
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
|
||||
};
|
||||
|
||||
use crate::color::RgbaColor;
|
||||
use crate::color::{is_valid_hex_color, RgbaColor};
|
||||
|
||||
use super::interface::{
|
||||
component::{Component, ComponentContext, RenderParams},
|
||||
|
@ -10,9 +10,6 @@ use super::interface::{
|
|||
style::{ComponentAlign, ComponentStyle, RawComponentStyle},
|
||||
};
|
||||
|
||||
const HEX_COLOR_LENGTH: usize = 7;
|
||||
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;
|
||||
|
||||
pub struct Background {
|
||||
children: Vec<Box<dyn Component>>,
|
||||
}
|
||||
|
@ -23,11 +20,6 @@ impl Background {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_valid_hex_color(color: String) -> bool {
|
||||
(color.len() == HEX_COLOR_LENGTH || color.len() == HEX_COLOR_WITH_ALPHA_LENGTH)
|
||||
&& color.starts_with("#")
|
||||
}
|
||||
|
||||
impl Component for Background {
|
||||
fn children(&self) -> &Vec<Box<dyn Component>> {
|
||||
&self.children
|
||||
|
@ -52,7 +44,7 @@ impl Component for Background {
|
|||
paint.anti_alias = false;
|
||||
match params.bg_color.as_ref() {
|
||||
Some(color) => {
|
||||
if ! is_valid_hex_color(color.to_string()){
|
||||
if !is_valid_hex_color(color) {
|
||||
return Err(RenderError::InvalidHexColor(color.to_string()));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue