feat: hex_color serialization for the theme

can also can deserialize the previous version of the theme, so existing themes should not be affected
This commit is contained in:
Ashley Wulber 2026-03-26 16:17:36 -04:00
parent 6653157def
commit 141bbd23ec
7 changed files with 264 additions and 25 deletions

View file

@ -15,6 +15,7 @@ export = ["serde_json"]
no-default = [] no-default = []
[dependencies] [dependencies]
hex_color = { version = "3", features = ["serde"] }
palette = { version = "0.7.6", features = ["serializing"] } palette = { version = "0.7.6", features = ["serializing"] }
almost = "0.2" almost = "0.2"
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }

View file

@ -0,0 +1,173 @@
//! Color representation and serde helpers for the Cosmic theme
use hex_color::HexColor;
use palette::{Srgb, Srgba};
use serde::{Deserialize, Serialize};
/// A color in the Cosmic theme for serialization and deserialization
#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ColorRepr {
/// A color represented as a hex string
#[serde(with = "hex_color::rgba")]
Hex(HexColor),
/// A color represented as an RGBA value
Rgba(Srgba),
/// A color represented as an RGB value
Rgb(Srgb),
}
/// An optional color in the Cosmic theme for serialization and deserialization
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct ColorReprOption(Option<ColorRepr>);
impl From<Srgb> for ColorRepr {
fn from(color: Srgb) -> Self {
let rgb_u8: Srgb<u8> = color.into_format();
ColorRepr::Hex(HexColor {
r: rgb_u8.red,
g: rgb_u8.green,
b: rgb_u8.blue,
a: 255,
})
}
}
impl From<Srgba> for ColorRepr {
fn from(color: Srgba) -> Self {
let rgba_u8: Srgba<u8> = color.into_format();
ColorRepr::Hex(HexColor {
r: rgba_u8.red,
g: rgba_u8.green,
b: rgba_u8.blue,
a: rgba_u8.alpha,
})
}
}
impl From<ColorRepr> for Srgb {
fn from(value: ColorRepr) -> Self {
match value {
ColorRepr::Hex(hex) => Srgb::<u8>::new(hex.r, hex.g, hex.b).into_format(),
ColorRepr::Rgb(rgb) => rgb,
ColorRepr::Rgba(rgba) => Srgb::new(rgba.red, rgba.green, rgba.blue),
}
}
}
impl From<ColorRepr> for Srgba {
fn from(value: ColorRepr) -> Self {
match value {
ColorRepr::Hex(hex) => Srgba::<u8>::new(hex.r, hex.g, hex.b, hex.a).into_format(),
ColorRepr::Rgb(rgb) => Srgba::new(rgb.red, rgb.green, rgb.blue, 1.0),
ColorRepr::Rgba(rgba) => rgba,
}
}
}
impl From<ColorReprOption> for Option<Srgb> {
fn from(value: ColorReprOption) -> Self {
value.0.map(std::convert::Into::into)
}
}
impl From<ColorReprOption> for Option<Srgba> {
fn from(value: ColorReprOption) -> Self {
value.0.map(std::convert::Into::into)
}
}
impl From<Option<Srgb>> for ColorReprOption {
fn from(value: Option<Srgb>) -> Self {
ColorReprOption(value.map(std::convert::Into::into))
}
}
impl From<Option<Srgba>> for ColorReprOption {
fn from(value: Option<Srgba>) -> Self {
ColorReprOption(value.map(std::convert::Into::into))
}
}
/// A trait for converting between a color type and its representation for serialization and deserialization
pub trait ConvColorRepr: Sized {
/// Convert from a color representation to the color type
fn from_repr(repr: ColorRepr) -> Self;
/// Convert from the color type to its representation for serialization
fn to_repr(&self) -> ColorRepr;
}
impl ConvColorRepr for Srgba {
fn from_repr(repr: ColorRepr) -> Self {
repr.into()
}
fn to_repr(&self) -> ColorRepr {
(*self).into()
}
}
impl ConvColorRepr for Srgb {
fn from_repr(repr: ColorRepr) -> Self {
repr.into()
}
fn to_repr(&self) -> ColorRepr {
(*self).into()
}
}
/// Serde helpers for serializing and deserializing colors in the Cosmic theme
pub mod color_serde {
use super::*;
use serde::{Deserialize, Deserializer, Serializer};
/// Serialize a color to a hex string
pub fn serialize<T, S>(color: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: ConvColorRepr,
S: Serializer,
{
let repr = color.to_repr();
repr.serialize(serializer)
}
/// Deserialize a color from a hex string or RGB/RGBA
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: ConvColorRepr,
D: Deserializer<'de>,
{
let repr = ColorRepr::deserialize(deserializer)?;
Ok(T::from_repr(repr))
}
/// Serde helpers for serializing and deserializing optional colors in the Cosmic theme
pub mod option {
use super::*;
/// Serialize an optional color
pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
where
T: ConvColorRepr,
S: Serializer,
{
match value {
Some(v) => super::serialize(v, serializer),
None => serializer.serialize_none(),
}
}
/// Deserialize an optional color
pub fn deserialize<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
where
T: ConvColorRepr,
D: Deserializer<'de>,
{
let opt = Option::<ColorRepr>::deserialize(deserializer)?;
Ok(opt.map(T::from_repr))
}
}
}

View file

@ -1,3 +1,4 @@
use crate::color::color_serde;
use palette::Srgba; use palette::Srgba;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::LazyLock; use std::sync::LazyLock;
@ -95,75 +96,107 @@ pub struct CosmicPaletteInner {
/// Utility Colors /// Utility Colors
/// Colors used for various points of emphasis in the UI. /// Colors used for various points of emphasis in the UI.
#[serde(with = "color_serde")]
pub bright_red: Srgba, pub bright_red: Srgba,
/// Colors used for various points of emphasis in the UI. /// Colors used for various points of emphasis in the UI.
#[serde(with = "color_serde")]
pub bright_green: Srgba, pub bright_green: Srgba,
/// Colors used for various points of emphasis in the UI. /// Colors used for various points of emphasis in the UI.
#[serde(with = "color_serde")]
pub bright_orange: Srgba, pub bright_orange: Srgba,
/// Surface Grays /// Surface Grays
/// Colors used for three levels of surfaces in the UI. /// Colors used for three levels of surfaces in the UI.
#[serde(with = "color_serde")]
pub gray_1: Srgba, pub gray_1: Srgba,
/// Colors used for three levels of surfaces in the UI. /// Colors used for three levels of surfaces in the UI.
#[serde(with = "color_serde")]
pub gray_2: Srgba, pub gray_2: Srgba,
/// System Neutrals /// System Neutrals
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_0: Srgba, pub neutral_0: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_1: Srgba, pub neutral_1: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_2: Srgba, pub neutral_2: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_3: Srgba, pub neutral_3: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_4: Srgba, pub neutral_4: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_5: Srgba, pub neutral_5: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_6: Srgba, pub neutral_6: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_7: Srgba, pub neutral_7: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_8: Srgba, pub neutral_8: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_9: Srgba, pub neutral_9: Srgba,
/// A wider spread of dark colors for more general use. /// A wider spread of dark colors for more general use.
#[serde(with = "color_serde")]
pub neutral_10: Srgba, pub neutral_10: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_blue: Srgba, pub accent_blue: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_indigo: Srgba, pub accent_indigo: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_purple: Srgba, pub accent_purple: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_pink: Srgba, pub accent_pink: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_red: Srgba, pub accent_red: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_orange: Srgba, pub accent_orange: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_yellow: Srgba, pub accent_yellow: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_green: Srgba, pub accent_green: Srgba,
/// Potential Accent Color Combos /// Potential Accent Color Combos
#[serde(with = "color_serde")]
pub accent_warm_grey: Srgba, pub accent_warm_grey: Srgba,
/// Extended Color Palette /// Extended Color Palette
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_warm_grey: Srgba, pub ext_warm_grey: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_orange: Srgba, pub ext_orange: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_yellow: Srgba, pub ext_yellow: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_blue: Srgba, pub ext_blue: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_purple: Srgba, pub ext_purple: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_pink: Srgba, pub ext_pink: Srgba,
/// Colors used for themes, app icons, illustrations, and other brand purposes. /// Colors used for themes, app icons, illustrations, and other brand purposes.
#[serde(with = "color_serde")]
pub ext_indigo: Srgba, pub ext_indigo: Srgba,
} }

View file

@ -1,3 +1,4 @@
use crate::color::color_serde;
use palette::{Srgba, WithAlpha}; use palette::{Srgba, WithAlpha};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -8,14 +9,18 @@ use crate::composite::over;
#[must_use] #[must_use]
pub struct Container { pub struct Container {
/// the color of the container /// the color of the container
#[serde(with = "color_serde")]
pub base: Srgba, pub base: Srgba,
/// the color of components in the container /// the color of components in the container
pub component: Component, pub component: Component,
/// the color of dividers in the container /// the color of dividers in the container
#[serde(with = "color_serde")]
pub divider: Srgba, pub divider: Srgba,
/// the color of text in the container /// the color of text in the container
#[serde(with = "color_serde")]
pub on: Srgba, pub on: Srgba,
/// the color of @small_widget_container /// the color of @small_widget_container
#[serde(with = "color_serde")]
pub small_widget: Srgba, pub small_widget: Srgba,
} }
@ -45,30 +50,42 @@ impl Container {
#[must_use] #[must_use]
pub struct Component { pub struct Component {
/// The base color of the widget /// The base color of the widget
#[serde(with = "color_serde")]
pub base: Srgba, pub base: Srgba,
/// The color of the widget when it is hovered /// The color of the widget when it is hovered
#[serde(with = "color_serde")]
pub hover: Srgba, pub hover: Srgba,
/// the color of the widget when it is pressed /// the color of the widget when it is pressed
#[serde(with = "color_serde")]
pub pressed: Srgba, pub pressed: Srgba,
/// the color of the widget when it is selected /// the color of the widget when it is selected
#[serde(with = "color_serde")]
pub selected: Srgba, pub selected: Srgba,
/// the color of the widget when it is selected /// the color of the widget when it is selected
#[serde(with = "color_serde")]
pub selected_text: Srgba, pub selected_text: Srgba,
/// the color of the widget when it is focused /// the color of the widget when it is focused
#[serde(with = "color_serde")]
pub focus: Srgba, pub focus: Srgba,
/// the color of dividers for this widget /// the color of dividers for this widget
#[serde(with = "color_serde")]
pub divider: Srgba, pub divider: Srgba,
/// the color of text for this widget /// the color of text for this widget
#[serde(with = "color_serde")]
pub on: Srgba, pub on: Srgba,
// the color of text with opacity 80 for this widget // the color of text with opacity 80 for this widget
// pub text_opacity_80: Srgba, // pub text_opacity_80: Srgba,
/// the color of the widget when it is disabled /// the color of the widget when it is disabled
#[serde(with = "color_serde")]
pub disabled: Srgba, pub disabled: Srgba,
/// the color of text in the widget when it is disabled /// the color of text in the widget when it is disabled
#[serde(with = "color_serde")]
pub on_disabled: Srgba, pub on_disabled: Srgba,
/// the color of the border for the widget /// the color of the border for the widget
#[serde(with = "color_serde")]
pub border: Srgba, pub border: Srgba,
/// the color of the border for the widget when it is disabled /// the color of the border for the widget when it is disabled
#[serde(with = "color_serde")]
pub disabled_border: Srgba, pub disabled_border: Srgba,
} }

View file

@ -6,6 +6,7 @@ pub use mode::*;
pub use spacing::*; pub use spacing::*;
pub use theme::*; pub use theme::*;
pub mod color;
mod corner; mod corner;
mod cosmic_palette; mod cosmic_palette;
mod density; mod density;

View file

@ -1,10 +1,11 @@
use crate::{ use crate::{
Component, Container, CornerRadii, CosmicPalette, CosmicPaletteInner, DARK_PALETTE, Component, Container, CornerRadii, CosmicPalette, CosmicPaletteInner, DARK_PALETTE,
LIGHT_PALETTE, NAME, Spacing, ThemeMode, LIGHT_PALETTE, NAME, Spacing, ThemeMode,
color::{ColorRepr, ColorReprOption, color_serde, color_serde::option as color_serde_option},
composite::over, composite::over,
steps::{color_index, get_small_widget_color, get_surface_color, get_text, steps}, steps::{color_index, get_small_widget_color, get_surface_color, get_text, steps},
}; };
use cosmic_config::{Config, CosmicConfigEntry}; use cosmic_config::{Config, CosmicConfigEntry, cosmic_config_derive::CosmicConfigEntry};
use palette::{ use palette::{
IntoColor, Oklcha, Srgb, Srgba, WithAlpha, color_difference::Wcag21RelativeContrast, rgb::Rgb, IntoColor, Oklcha, Srgb, Srgba, WithAlpha, color_difference::Wcag21RelativeContrast, rgb::Rgb,
}; };
@ -37,15 +38,8 @@ pub enum Layer {
#[must_use] #[must_use]
/// Cosmic Theme data structure with all colors and its name /// Cosmic Theme data structure with all colors and its name
#[derive( #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, CosmicConfigEntry)]
Clone, #[version = 2]
Debug,
Serialize,
Deserialize,
PartialEq,
cosmic_config::cosmic_config_derive::CosmicConfigEntry,
)]
#[version = 1]
pub struct Theme { pub struct Theme {
/// name of the theme /// name of the theme
pub name: String, pub name: String,
@ -98,13 +92,21 @@ pub struct Theme {
/// enables blurred transparency /// enables blurred transparency
pub is_frosted: bool, pub is_frosted: bool,
/// shade color for dialogs /// shade color for dialogs
#[serde(with = "color_serde")]
#[cosmic_config_entry(with = ColorRepr)]
pub shade: Srgba, pub shade: Srgba,
/// accent text colors /// accent text colors
/// If None, accent base color is the accent text color. /// If None, accent base color is the accent text color.
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub accent_text: Option<Srgba>, pub accent_text: Option<Srgba>,
/// control tint color /// control tint color
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub control_tint: Option<Srgb>, pub control_tint: Option<Srgb>,
/// text tint color /// text tint color
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub text_tint: Option<Srgb>, pub text_tint: Option<Srgb>,
} }
@ -739,7 +741,7 @@ impl Theme {
if color_scheme.trim().contains("default") || color_scheme.trim().contains("light") { if color_scheme.trim().contains("default") || color_scheme.trim().contains("light") {
return Self::light_default(); return Self::light_default();
} }
}; }
Self::dark_default() Self::dark_default()
} }
@ -748,10 +750,10 @@ impl Theme {
pub fn preferred_theme() -> Self { pub fn preferred_theme() -> Self {
let current_desktop = std::env::var("XDG_CURRENT_DESKTOP"); let current_desktop = std::env::var("XDG_CURRENT_DESKTOP");
if let Ok(desktop) = current_desktop { if let Ok(desktop) = current_desktop
if desktop.trim().to_lowercase().contains("gnome") { && desktop.trim().to_lowercase().contains("gnome")
return Self::gtk_prefer_colorscheme(); {
} return Self::gtk_prefer_colorscheme();
} }
Self::dark_default() Self::dark_default()
@ -766,15 +768,8 @@ impl From<CosmicPalette> for Theme {
#[must_use] #[must_use]
/// Helper for building customized themes /// Helper for building customized themes
#[derive( #[derive(Clone, Debug, Serialize, Deserialize, CosmicConfigEntry, PartialEq)]
Clone, #[version = 2]
Debug,
Serialize,
Deserialize,
cosmic_config::cosmic_config_derive::CosmicConfigEntry,
PartialEq,
)]
#[version = 1]
pub struct ThemeBuilder { pub struct ThemeBuilder {
/// override the palette for the builder /// override the palette for the builder
pub palette: CosmicPalette, pub palette: CosmicPalette,
@ -783,22 +778,40 @@ pub struct ThemeBuilder {
/// override corner radii for the builder /// override corner radii for the builder
pub corner_radii: CornerRadii, pub corner_radii: CornerRadii,
/// override neutral_tint for the builder /// override neutral_tint for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub neutral_tint: Option<Srgb>, pub neutral_tint: Option<Srgb>,
/// override bg_color for the builder /// override bg_color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub bg_color: Option<Srgba>, pub bg_color: Option<Srgba>,
/// override the primary container bg color for the builder /// override the primary container bg color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub primary_container_bg: Option<Srgba>, pub primary_container_bg: Option<Srgba>,
/// override the secontary container bg color for the builder /// override the secontary container bg color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub secondary_container_bg: Option<Srgba>, pub secondary_container_bg: Option<Srgba>,
/// override the text tint for the builder /// override the text tint for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub text_tint: Option<Srgb>, pub text_tint: Option<Srgb>,
/// override the accent color for the builder /// override the accent color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub accent: Option<Srgb>, pub accent: Option<Srgb>,
/// override the success color for the builder /// override the success color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub success: Option<Srgb>, pub success: Option<Srgb>,
/// override the warning color for the builder /// override the warning color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub warning: Option<Srgb>, pub warning: Option<Srgb>,
/// override the destructive color for the builder /// override the destructive color for the builder
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub destructive: Option<Srgb>, pub destructive: Option<Srgb>,
/// enabled blurred transparency /// enabled blurred transparency
pub is_frosted: bool, // TODO handle pub is_frosted: bool, // TODO handle
@ -807,6 +820,8 @@ pub struct ThemeBuilder {
/// cosmic-comp active hint window outline width /// cosmic-comp active hint window outline width
pub active_hint: u32, pub active_hint: u32,
/// cosmic-comp custom window hint color /// cosmic-comp custom window hint color
#[serde(with = "color_serde_option")]
#[cosmic_config_entry(with = ColorReprOption)]
pub window_hint: Option<Srgb>, pub window_hint: Option<Srgb>,
} }

View file

@ -65,7 +65,6 @@ pub fn file_transfer_send(
/// Returns a list of file paths. /// Returns a list of file paths.
#[cfg(feature = "xdg-portal")] #[cfg(feature = "xdg-portal")]
pub fn file_transfer_receive(key: String) -> iced::Task<ashpd::Result<Vec<String>>> { pub fn file_transfer_receive(key: String) -> iced::Task<ashpd::Result<Vec<String>>> {
dbg!(&key);
iced::Task::future(async move { iced::Task::future(async move {
let file_transfer = ashpd::documents::FileTransfer::new().await?; let file_transfer = ashpd::documents::FileTransfer::new().await?;
file_transfer.retrieve_files(&key).await file_transfer.retrieve_files(&key).await