Skip to content

Commit

Permalink
[Feat] update background format
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Oct 27, 2024
1 parent 69a5362 commit fa3c4f0
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 77 deletions.
117 changes: 117 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
workspace = { members = ["cli", "core"] }
[workspace]
resolver = "2"

members = ["cli", "core"]
59 changes: 2 additions & 57 deletions core/src/components/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tiny_skia::{

use crate::{
edges::{edge::Edge, padding::Padding},
utils::color::RgbaColor,
utils::{color::RgbaColor, helpers::convert_vecs},
};

use super::interface::{
Expand Down Expand Up @@ -71,29 +71,14 @@ impl Component for Background {
paint.shader = LinearGradient::new(
Point::from_xy(start.x, start.y),
Point::from_xy(end.x, end.y),
gradient_background.stops.clone(),
convert_vecs(gradient_background.stops.clone()),
SpreadMode::Pad,
Transform::identity(),
)
.unwrap();
}
};

// if is_valid_hex_color(&params.background) {
// let rgba_color: RgbaColor = params.background.as_str().into();
//
// paint.set_color(rgba_color.into());
// } else {
// paint.shader = LinearGradient::new(
// Point::from_xy(0., 0.),
// Point::from_xy(w, 0.),
// Background::get_theme(&params.background)?,
// SpreadMode::Pad,
// Transform::identity(),
// )
// .unwrap();
// }

pixmap.fill_rect(
Rect::from_xywh(0., 0., w, h).unwrap(),
&paint,
Expand All @@ -104,43 +89,3 @@ impl Component for Background {
Ok(())
}
}

impl Background {
fn get_theme(theme: &str) -> render_error::Result<Vec<GradientStop>> {
let theme = match theme {
"default" => vec![
GradientStop::new(0.0, Color::from_rgba8(58, 28, 113, 255)),
GradientStop::new(0.5, Color::from_rgba8(215, 109, 119, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 175, 123, 255)),
],
"sea" => vec![
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.95, Color::from_rgba8(166, 255, 203, 255)),
],
"grape" => vec![
GradientStop::new(0.28, Color::from_rgba8(103, 90, 247, 255)),
GradientStop::new(0.95, Color::from_rgba8(189, 101, 250, 255)),
],
"peach" => vec![
GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)),
GradientStop::new(0.95, Color::from_rgba8(247, 187, 151, 255)),
],
"summer" => vec![
GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)),
GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)),
],
"bamboo" => vec![
GradientStop::new(0.22, Color::from_rgba8(107, 203, 165, 255)),
GradientStop::new(0.95, Color::from_rgba8(202, 244, 194, 255)),
],
"dusk" => vec![
GradientStop::new(0.22, Color::from_rgba8(255, 98, 110, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 190, 113, 255)),
],
_ => return Err(RenderError::UnknownBackgroundTheme(theme.to_string())),
};

Ok(theme)
}
}
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
mod components;
pub mod config;
pub mod edges;
pub mod preset_themes;
pub mod preset_background;
pub mod snapshot;
pub mod utils;
37 changes: 19 additions & 18 deletions core/src/preset_themes.rs → core/src/preset_background.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use once_cell::sync::Lazy;
use tiny_skia::{Color, GradientStop};

use crate::config::{Background, DimensionValue, GradientPoint, LinearGradient};
use crate::config::{
Background, DimensionValue, GradientPoint, LinearGradient, LinearGradientStop,
};

// This file contains the preset background themes that can be used in CodeSnap
// If you want to add more beautiful background themes, please feel free to contribute
Expand All @@ -17,8 +18,8 @@ pub static BAMBOO: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.22, Color::from_rgba8(107, 203, 165, 255)),
GradientStop::new(0.95, Color::from_rgba8(202, 244, 194, 255)),
LinearGradientStop::new(0.22, "#6bcba5"),
LinearGradientStop::new(0.95, "#caf4c2"),
],
})
});
Expand All @@ -34,9 +35,9 @@ pub static SEA: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
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.95, Color::from_rgba8(166, 255, 203, 255)),
LinearGradientStop::new(0., "#1fa2ff"),
LinearGradientStop::new(0.4, "#12d8fa"),
LinearGradientStop::new(0.95, "#a6ffcb"),
],
})
});
Expand All @@ -53,9 +54,9 @@ pub static CLASSIC: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.0, Color::from_rgba8(58, 28, 113, 255)),
GradientStop::new(0.5, Color::from_rgba8(215, 109, 119, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 175, 123, 255)),
LinearGradientStop::new(0.0, "#3a1c71"),
LinearGradientStop::new(0.5, "#d76d77"),
LinearGradientStop::new(0.95, "#ffb07c"),
],
})
});
Expand All @@ -71,8 +72,8 @@ pub static GRAPE: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.28, Color::from_rgba8(103, 90, 247, 255)),
GradientStop::new(0.95, Color::from_rgba8(189, 101, 250, 255)),
LinearGradientStop::new(0.28, "#675af7"),
LinearGradientStop::new(0.95, "#bd65fa"),
],
})
});
Expand All @@ -88,8 +89,8 @@ pub static PEACH: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.22, Color::from_rgba8(221, 94, 137, 255)),
GradientStop::new(0.95, Color::from_rgba8(247, 187, 151, 255)),
LinearGradientStop::new(0.22, "#dd5e89"),
LinearGradientStop::new(0.95, "#f7bb97"),
],
})
});
Expand All @@ -105,8 +106,8 @@ pub static SUMMER: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.28, Color::from_rgba8(248, 165, 194, 255)),
GradientStop::new(0.95, Color::from_rgba8(116, 185, 255, 255)),
LinearGradientStop::new(0.28, "#f8a5c2"),
LinearGradientStop::new(0.95, "#74b9ff"),
],
})
});
Expand All @@ -122,8 +123,8 @@ pub static DUSK: Lazy<Background> = Lazy::new(|| {
y: DimensionValue::Num(0.),
},
stops: vec![
GradientStop::new(0.22, Color::from_rgba8(255, 98, 110, 255)),
GradientStop::new(0.95, Color::from_rgba8(255, 190, 113, 255)),
LinearGradientStop::new(0.22, "#ff626e"),
LinearGradientStop::new(0.95, "#ffbe71"),
],
})
});
2 changes: 2 additions & 0 deletions core/src/utils/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ impl<'a> ImageRefMut<'a> {
}
}

#[allow(dead_code)]
#[inline]
fn pixel_at(&self, x: u32, y: u32) -> RGBA8 {
self.data[(self.width * y + x) as usize]
}

#[allow(dead_code)]
#[inline]
fn pixel_at_mut(&mut self, x: u32, y: u32) -> &mut RGBA8 {
&mut self.data[(self.width * y + x) as usize]
Expand Down
6 changes: 6 additions & 0 deletions core/src/utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn convert_vecs<T, U>(v: Vec<T>) -> Vec<U>
where
T: Into<U>,
{
v.into_iter().map(Into::into).collect()
}

0 comments on commit fa3c4f0

Please sign in to comment.