Skip to content

Commit

Permalink
refactor: small widget container colors
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 authored and mmstick committed Feb 17, 2025
1 parent 25bf8f6 commit 3f25af8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
38 changes: 10 additions & 28 deletions cosmic-theme/src/model/theme.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
composite::over,
steps::{color_index, get_index, get_surface_color, get_text, steps},
steps::{color_index, get_index, get_small_widget_color, get_surface_color, get_text, steps},
Component, Container, CornerRadii, CosmicPalette, CosmicPaletteInner, Spacing, ThemeMode,
DARK_PALETTE, LIGHT_PALETTE, NAME,
};
Expand Down Expand Up @@ -847,7 +847,7 @@ impl ThemeBuilder {
color_index(bg_component, step_array.len()),
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
text_steps_array.as_deref(),
);

let primary = {
Expand Down Expand Up @@ -879,7 +879,7 @@ impl ThemeBuilder {
color_index(component_base, step_array.len()),
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
text_steps_array.as_deref(),
),
component_hovered_overlay,
component_pressed_overlay,
Expand All @@ -891,15 +891,9 @@ impl ThemeBuilder {
base_index,
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
),
get_surface_color(
base_index,
5,
&neutral_steps,
base_index <= 65,
&p_ref.neutral_6,
text_steps_array.as_deref(),
),
get_small_widget_color(base_index, 5, &neutral_steps, &p_ref.neutral_6),
);

container
Expand Down Expand Up @@ -971,15 +965,9 @@ impl ThemeBuilder {
bg_index,
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
),
get_surface_color(
bg_index,
5,
&neutral_steps,
bg_index <= 65,
&p_ref.neutral_6,
text_steps_array.as_deref(),
),
get_small_widget_color(bg_index, 5, &neutral_steps, &p_ref.neutral_6),
),
primary,
secondary: {
Expand Down Expand Up @@ -1011,7 +999,7 @@ impl ThemeBuilder {
color_index(secondary_component, step_array.len()),
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
text_steps_array.as_deref(),
),
component_hovered_overlay,
component_pressed_overlay,
Expand All @@ -1023,15 +1011,9 @@ impl ThemeBuilder {
base_index,
&step_array,
&p_ref.neutral_8,
text_steps_array.as_ref(),
),
get_surface_color(
base_index,
5,
&neutral_steps,
base_index <= 65,
&p_ref.neutral_6,
text_steps_array.as_deref(),
),
get_small_widget_color(base_index, 5, &neutral_steps, &p_ref.neutral_6),
)
},
accent: Component::colored_component(
Expand Down
34 changes: 30 additions & 4 deletions cosmic-theme/src/steps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::num::NonZeroUsize;

use almost::equal;
use palette::{convert::FromColorUnclamped, ClampAssign, FromColor, Oklcha, Srgb, Srgba};
use palette::{convert::FromColorUnclamped, ClampAssign, FromColor, Lch, Oklcha, Srgb, Srgba};

/// Get an array of 100 colors with a specific hue and chroma
/// over the full range of lightness.
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn get_index(base_index: usize, steps: usize, step_len: usize, is_dark: bool
pub fn get_surface_color(
base_index: usize,
steps: usize,
step_array: &Vec<Srgba>,
step_array: &[Srgba],
mut is_dark: bool,
fallback: &Srgba,
) -> Srgba {
Expand All @@ -48,12 +48,38 @@ pub fn get_surface_color(
.unwrap_or(fallback)
}

/// get surface color given a base and some steps
#[must_use]
pub fn get_small_widget_color(
base_index: usize,
steps: usize,
step_array: &[Srgba],
fallback: &Srgba,
) -> Srgba {
assert!(step_array.len() == 100);

let is_dark = base_index <= 40 || (base_index > 51 && base_index < 65);

let res = *get_index(base_index, steps, step_array.len(), is_dark)
.and_then(|i| step_array.get(i))
.unwrap_or(fallback);

let mut lch = Lch::from_color(res);
if lch.chroma / Lch::<f32>::max_chroma() > 0.03 {
lch.chroma = 0.03 * Lch::<f32>::max_chroma();
lch.clamp_assign();
Srgba::from_color(lch)
} else {
res
}
}

/// get text color given a base background color
pub fn get_text(
base_index: usize,
step_array: &Vec<Srgba>,
step_array: &[Srgba],
fallback: &Srgba,
tint_array: Option<&Vec<Srgba>>,
tint_array: Option<&[Srgba]>,
) -> Srgba {
assert!(step_array.len() == 100);
let step_array = if let Some(tint_array) = tint_array {
Expand Down

0 comments on commit 3f25af8

Please sign in to comment.