forked from mirror/codesnap.nvim
[Feat] convert rgb hex color to rgba hex color
parent
8a05bddadb
commit
14d2445c32
|
@ -1,27 +1,44 @@
|
||||||
use std::i64;
|
use std::i64;
|
||||||
use tiny_skia::Color;
|
use tiny_skia::Color;
|
||||||
|
|
||||||
|
const HEX_COLOR_LENGTH: usize = 7;
|
||||||
|
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;
|
||||||
|
|
||||||
pub struct RgbaColor {
|
pub struct RgbaColor {
|
||||||
pub color: Color,
|
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 {
|
impl Into<RgbaColor> for String {
|
||||||
fn into(self) -> RgbaColor {
|
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 chars = hex_color.chars().collect::<Vec<char>>();
|
||||||
let splits = &chars
|
let splits = &chars
|
||||||
.chunks(2)
|
.chunks(2)
|
||||||
.map(|chunk| i64::from_str_radix(&chunk.iter().collect::<String>(), 16).unwrap())
|
.map(|chunk| i64::from_str_radix(&chunk.iter().collect::<String>(), 16).unwrap())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let alpha: i64;
|
|
||||||
match splits.get(3) {
|
|
||||||
Some(x) => alpha = *x,
|
|
||||||
None => alpha = 255,
|
|
||||||
}
|
|
||||||
|
|
||||||
RgbaColor {
|
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,
|
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::color::RgbaColor;
|
use crate::color::{is_valid_hex_color, RgbaColor};
|
||||||
|
|
||||||
use super::interface::{
|
use super::interface::{
|
||||||
component::{Component, ComponentContext, RenderParams},
|
component::{Component, ComponentContext, RenderParams},
|
||||||
|
@ -10,9 +10,6 @@ use super::interface::{
|
||||||
style::{ComponentAlign, ComponentStyle, RawComponentStyle},
|
style::{ComponentAlign, ComponentStyle, RawComponentStyle},
|
||||||
};
|
};
|
||||||
|
|
||||||
const HEX_COLOR_LENGTH: usize = 7;
|
|
||||||
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;
|
|
||||||
|
|
||||||
pub struct Background {
|
pub struct Background {
|
||||||
children: Vec<Box<dyn Component>>,
|
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 {
|
impl Component for Background {
|
||||||
fn children(&self) -> &Vec<Box<dyn Component>> {
|
fn children(&self) -> &Vec<Box<dyn Component>> {
|
||||||
&self.children
|
&self.children
|
||||||
|
@ -52,7 +44,7 @@ impl Component for Background {
|
||||||
paint.anti_alias = false;
|
paint.anti_alias = false;
|
||||||
match params.bg_color.as_ref() {
|
match params.bg_color.as_ref() {
|
||||||
Some(color) => {
|
Some(color) => {
|
||||||
if ! is_valid_hex_color(color.to_string()){
|
if !is_valid_hex_color(color) {
|
||||||
return Err(RenderError::InvalidHexColor(color.to_string()));
|
return Err(RenderError::InvalidHexColor(color.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue