Skip to main content

kernel/
color.rs

1// Copyright 2026 Mark AB (markik)
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Portable RGBA color primitive.
5//!
6//! Pre-Slice-52, this type lived inside
7//! `shell::desktop::runtime::registries::theme` with a feature-flag
8//! switch that aliased to `egui::Color32` under `egui-host`. That made
9//! every portable consumer (knowledge registry, lens registry,
10//! presentation, edge styles) reach into the shell-side runtime tree
11//! to import a primitive — backwards layering. Slice 52 promotes the
12//! portable form to `kernel::color::Color32`. The egui-host
13//! re-export at theme.rs continues to alias to `egui::Color32` (the
14//! egui-host build is frozen per the iced jump-ship plan §S1, so its
15//! integration with `egui::Color32` is unchanged).
16
17use serde::{Deserialize, Serialize};
18
19/// Portable 32-bit RGBA color (`[r, g, b, a]` bytes, alpha in
20/// `0..=255`). Constructors mirror the API of `egui::Color32` so the
21/// migration from the egui-flavoured original is mechanical.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
23pub struct Color32([u8; 4]);
24
25impl Color32 {
26    pub const BLACK: Self = Self([0, 0, 0, 255]);
27    pub const GRAY: Self = Self([128, 128, 128, 255]);
28    pub const TRANSPARENT: Self = Self([0, 0, 0, 0]);
29    pub const WHITE: Self = Self([255, 255, 255, 255]);
30
31    pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
32        Self([r, g, b, 255])
33    }
34
35    pub const fn from_rgba_unmultiplied(r: u8, g: u8, b: u8, a: u8) -> Self {
36        Self([r, g, b, a])
37    }
38
39    pub const fn from_gray(value: u8) -> Self {
40        Self([value, value, value, 255])
41    }
42
43    pub const fn r(self) -> u8 {
44        self.0[0]
45    }
46
47    pub const fn g(self) -> u8 {
48        self.0[1]
49    }
50
51    pub const fn b(self) -> u8 {
52        self.0[2]
53    }
54
55    pub const fn a(self) -> u8 {
56        self.0[3]
57    }
58
59    pub const fn to_array(self) -> [u8; 4] {
60        self.0
61    }
62}