Skip to main content

iced_pywal/
lib.rs

1use std::io::Read;
2
3fn colr(colors : &[Vec<u8>], id : usize) -> iced::Color{
4    iced::Color::from_rgb8(colors[id][0], colors[id][1], colors[id][2])
5}
6fn pywal_palette(colors : &[Vec<u8>]) -> iced::theme::Palette{
7    let mut pallete = iced::Theme::Dark.palette();
8    {
9        pallete.background = colr(colors, 0);
10        pallete.primary = colr(colors, 4);
11        pallete.success = colr(colors, 2);
12        pallete.danger = colr(colors, 1);
13        pallete.text = colr(colors, 7);
14    }
15    pallete
16}
17fn get_pywal_colors() -> Result<Vec<Vec<u8>>, Box<dyn std::error::Error>>{
18    let path = std::env::home_dir()
19        .unwrap_or_default()
20        .to_string_lossy()
21        .to_string()
22        + "/.cache/wal/colors-rgb";
23    let mut colors = std::fs::File::open(&path)?;
24    let mut colors_string = "".to_string();
25    colors.read_to_string(&mut colors_string)?;
26    Ok(colors_string
27        .lines()
28        .map(|line| {
29            line.split(",")
30                .map(|l| l.parse::<u8>().unwrap_or(0))
31                .collect::<Vec<_>>()
32        })
33        .collect::<Vec<_>>())
34}
35pub fn color_scheme() -> Result<iced::Theme, Box<dyn std::error::Error>> {
36    let colors = get_pywal_colors()?;
37    let palette = pywal_palette(&colors);
38    let theme = iced::Theme::custom("pywal".to_string(), palette);
39    Ok(theme)
40}
41