[Feat] support bg_color config

pull/49/head
Mist 2024-03-17 18:47:43 +08:00
parent 3f57ce62fc
commit f3cbc3b901
5 changed files with 67 additions and 21 deletions

21
generator/src/color.rs Normal file
View File

@ -0,0 +1,21 @@
use std::i64;
use tiny_skia::Color;
pub struct RgbaColor {
pub color: Color,
}
impl Into<RgbaColor> for String {
fn into(self) -> RgbaColor {
let hex_color = &self.to_lowercase()[1..self.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<_>>();
RgbaColor {
color: Color::from_rgba8(splits[0] as u8, splits[1] as u8, splits[2] as u8, 255),
}
}
}

View File

@ -2,12 +2,16 @@ 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 super::interface::{ use super::interface::{
component::{Component, ComponentContext, RenderParams}, component::{Component, ComponentContext, RenderParams},
render_error::{self, RenderError}, render_error::{self, RenderError},
style::{ComponentAlign, ComponentStyle, RawComponentStyle}, style::{ComponentAlign, ComponentStyle, RawComponentStyle},
}; };
const HEX_COLOR_LENGTH: usize = 7;
pub struct Background { pub struct Background {
children: Vec<Box<dyn Component>>, children: Vec<Box<dyn Component>>,
} }
@ -37,8 +41,20 @@ impl Component for Background {
let mut paint = Paint::default(); let mut paint = Paint::default();
let w = pixmap.width() as f32; let w = pixmap.width() as f32;
let h = pixmap.height() as f32; let h = pixmap.height() as f32;
let params = &context.take_snapshot_params;
paint.anti_alias = false; paint.anti_alias = false;
match params.bg_color.as_ref() {
Some(color) => {
if color.len() != HEX_COLOR_LENGTH || !color.starts_with("#") {
return Err(RenderError::InvalidHexColor(color.to_string()));
}
let rgba_color: RgbaColor = color.to_string().into();
paint.set_color(rgba_color.color);
}
None => {
paint.shader = LinearGradient::new( paint.shader = LinearGradient::new(
Point::from_xy(0., 0.), Point::from_xy(0., 0.),
Point::from_xy(w, 0.), Point::from_xy(w, 0.),
@ -47,6 +63,8 @@ impl Component for Background {
Transform::identity(), Transform::identity(),
) )
.unwrap(); .unwrap();
}
};
pixmap.fill_rect( pixmap.fill_rect(
Rect::from_xywh(0., 0., w, h).unwrap(), Rect::from_xywh(0., 0., w, h).unwrap(),
@ -67,29 +85,31 @@ impl Background {
GradientStop::new(0.5, Color::from_rgba8(215, 109, 119, 255)), GradientStop::new(0.5, Color::from_rgba8(215, 109, 119, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 175, 123, 255)), GradientStop::new(0.95, Color::from_rgba8(255, 175, 123, 255)),
], ],
"cyan" => vec![ "sea" => vec![
GradientStop::new(0.0, Color::from_rgba8(31, 162, 255, 255)), GradientStop::new(0.0, Color::from_rgba8(31, 162, 255, 255)),
GradientStop::new(0.4, Color::from_rgba8(18, 216, 250, 255)), GradientStop::new(0.4, Color::from_rgba8(18, 216, 250, 255)),
GradientStop::new(0.95, Color::from_rgba8(166, 255, 203, 255)), GradientStop::new(0.95, Color::from_rgba8(166, 255, 203, 255)),
], ],
"rose" => vec![ "grape" => vec![
GradientStop::new(0.1, Color::from_rgba8(180, 101, 218, 255)), GradientStop::new(0.28, Color::from_rgba8(103, 90, 247, 255)),
GradientStop::new(0.33, Color::from_rgba8(207, 108, 201, 255)), GradientStop::new(0.95, Color::from_rgba8(189, 101, 250, 255)),
GradientStop::new(0.66, Color::from_rgba8(238, 96, 156, 255)),
GradientStop::new(1., Color::from_rgba8(238, 96, 156, 255)),
], ],
"pink" => vec![ "peach" => vec![
GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)), GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)),
GradientStop::new(0.55, Color::from_rgba8(247, 187, 151, 255)), GradientStop::new(0.95, Color::from_rgba8(247, 187, 151, 255)),
], ],
"yellow" => vec![ "summer" => vec![
GradientStop::new(0., Color::from_rgba8(85, 239, 196, 255)),
GradientStop::new(0.4, Color::from_rgba8(255, 234, 167, 255)),
],
"blue" => vec![
GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)), GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)),
GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)), GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)),
], ],
"bamboo" => vec![
GradientStop::new(0.1, Color::from_rgba8(107, 203, 165, 255)),
GradientStop::new(95., Color::from_rgba8(202, 244, 194, 255)),
],
"dusk" => vec![
GradientStop::new(0.2, Color::from_rgba8(224, 103, 76, 255)),
GradientStop::new(95., Color::from_rgba8(253, 175, 25, 255)),
],
_ => return Err(RenderError::UnknownBackgroundTheme(theme.to_string())), _ => return Err(RenderError::UnknownBackgroundTheme(theme.to_string())),
}; };

View File

@ -13,6 +13,9 @@ pub enum RenderError {
#[error("Unable to parse unknown background theme {0}")] #[error("Unable to parse unknown background theme {0}")]
UnknownBackgroundTheme(String), UnknownBackgroundTheme(String),
#[error("Invalid hex color {0}")]
InvalidHexColor(String),
} }
impl From<RenderError> for nvim_oxi::Error { impl From<RenderError> for nvim_oxi::Error {

View File

@ -18,6 +18,7 @@ pub struct TakeSnapshotParams {
pub fonts_folder: String, pub fonts_folder: String,
pub theme: String, pub theme: String,
pub bg_theme: String, pub bg_theme: String,
pub bg_color: Option<String>,
} }
impl FromObject for TakeSnapshotParams { impl FromObject for TakeSnapshotParams {

View File

@ -1,4 +1,5 @@
mod code; mod code;
mod color;
mod components; mod components;
mod config; mod config;
mod copy; mod copy;