Emit accurate hex colors in generated themes (#3547)

This PR fixes an issues where the hex colors in the generated themes
were not correct.

We're using the [`palette`](https://crates.io/crates/palette) crate to
perform the conversions, as this gives us accurate hex codes that match
the VS Code source themes.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2023-12-07 18:34:03 -05:00 committed by GitHub
parent 0b78a401de
commit c7e19c0bcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 974 additions and 871 deletions

View file

@ -13,6 +13,7 @@ gpui = { package = "gpui2", path = "../gpui2" }
indexmap = "1.6.2"
json_comments = "0.2.2"
log.workspace = true
palette = { version = "0.7.3", default-features = false, features = ["std"] }
rust-embed.workspace = true
serde.workspace = true
simplelog = "0.9"

View file

@ -0,0 +1,54 @@
use anyhow::Result;
use gpui::Hsla;
use palette::FromColor;
pub(crate) fn try_parse_color(color: &str) -> Result<Hsla> {
let rgba = gpui::Rgba::try_from(color)?;
let rgba = palette::rgb::Srgba::from_components((rgba.r, rgba.g, rgba.b, rgba.a));
let hsla = palette::Hsla::from_color(rgba);
let hsla = gpui::hsla(
hsla.hue.into_positive_degrees() / 360.,
hsla.saturation,
hsla.lightness,
hsla.alpha,
);
Ok(hsla)
}
pub(crate) fn pack_color(color: Hsla) -> u32 {
let hsla = palette::Hsla::from_components((color.h * 360., color.s, color.l, color.a));
let rgba = palette::rgb::Srgba::from_color(hsla);
let rgba = rgba.into_format::<u8, u8>();
u32::from(rgba)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_serialize_color() {
let color = "#b4637aff";
let hsla = try_parse_color(color).unwrap();
let packed = pack_color(hsla);
assert_eq!(format!("#{:x}", packed), color);
}
#[test]
pub fn test_serialize_color_with_palette() {
let color = "#b4637aff";
let rgba = gpui::Rgba::try_from(color).unwrap();
let rgba = palette::rgb::Srgba::from_components((rgba.r, rgba.g, rgba.b, rgba.a));
let hsla = palette::Hsla::from_color(rgba);
let rgba = palette::rgb::Srgba::from_color(hsla);
let rgba = rgba.into_format::<u8, u8>();
assert_eq!(format!("#{:x}", rgba), color);
}
}

View file

@ -1,3 +1,4 @@
mod color;
mod theme_printer;
mod util;
mod vscode;

View file

@ -1,12 +1,14 @@
use std::fmt::{self, Debug};
use gpui::{Hsla, Rgba};
use gpui::Hsla;
use theme::{
Appearance, PlayerColor, PlayerColors, StatusColorsRefinement, SystemColors,
ThemeColorsRefinement, UserHighlightStyle, UserSyntaxTheme, UserTheme, UserThemeFamily,
UserThemeStylesRefinement,
};
use crate::color::pack_color;
struct RawSyntaxPrinter<'a>(&'a str);
impl<'a> Debug for RawSyntaxPrinter<'a> {
@ -19,7 +21,7 @@ struct HslaPrinter(Hsla);
impl Debug for HslaPrinter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", IntoPrinter(&Rgba::from(self.0)))
write!(f, "rgba({:#010x}).into()", pack_color(self.0))
}
}

View file

@ -1,5 +1,5 @@
use anyhow::Result;
use gpui::{rgba, Hsla, Rgba};
use gpui::rgba;
use indexmap::IndexMap;
use strum::IntoEnumIterator;
use theme::{
@ -7,16 +7,13 @@ use theme::{
UserHighlightStyle, UserSyntaxTheme, UserTheme, UserThemeStylesRefinement,
};
use crate::color::try_parse_color;
use crate::util::Traverse;
use crate::vscode::VsCodeTheme;
use crate::ThemeMetadata;
use super::ZedSyntaxToken;
pub(crate) fn try_parse_color(color: &str) -> Result<Hsla> {
Ok(Rgba::try_from(color)?.into())
}
pub(crate) fn try_parse_font_weight(font_style: &str) -> Option<UserFontWeight> {
match font_style {
style if style.contains("bold") => Some(UserFontWeight::BOLD),