color.rs

 1use gpui::{App, Hsla};
 2use theme::ActiveTheme;
 3
 4/// Sets a color that has a consistent meaning across all themes.
 5#[derive(Debug, Default, Eq, PartialEq, Copy, Clone)]
 6pub enum Color {
 7    #[default]
 8    /// The default text color. Might be known as "foreground" or "primary" in
 9    /// some theme systems.
10    ///
11    /// For less emphasis, consider using [`Color::Muted`] or [`Color::Hidden`].
12    Default,
13    /// A text color used for accents, such as links or highlights.
14    Accent,
15    /// A color used to indicate a conflict, such as a version control merge conflict, or a conflict between a file in the editor and the file system.
16    Conflict,
17    /// A color used to indicate a newly created item, such as a new file in
18    /// version control, or a new file on disk.
19    Created,
20    /// It is highly, HIGHLY recommended not to use this! Using this color
21    /// means detaching it from any semantic meaning across themes.
22    ///
23    /// A custom color specified by an HSLA value.
24    Custom(Hsla),
25    /// A color used to indicate a deleted item, such as a file removed from version control.
26    Deleted,
27    /// A color used for disabled UI elements or text, like a disabled button or menu item.
28    Disabled,
29    /// A color used to indicate an error condition, or something the user
30    /// cannot do. In very rare cases, it might be used to indicate dangerous or
31    /// destructive action.
32    Error,
33    /// A color used for elements that represent something that is hidden, like
34    /// a hidden file, or an element that should be visually de-emphasized.
35    Hidden,
36    /// A color used for hint or suggestion text, often a blue color. Use this
37    /// color to represent helpful, or semantically neutral information.
38    Hint,
39    /// A color used for items that are intentionally ignored, such as files ignored by version control.
40    Ignored,
41    /// A color used for informational messages or status indicators, often a blue color.
42    Info,
43    /// A color used to indicate a modified item, such as an edited file, or a modified entry in version control.
44    Modified,
45    /// A color used for text or UI elements that should be visually muted or de-emphasized.
46    ///
47    /// For more emphasis, consider using [`Color::Default`].
48    ///
49    /// For less emphasis, consider using [`Color::Hidden`].
50    Muted,
51    /// A color used for placeholder text in input fields.
52    Placeholder,
53    /// A color associated with a specific player number.
54    Player(u32),
55    /// A color used to indicate selected text or UI elements.
56    Selected,
57    /// A color used to indicate a successful operation or status.
58    Success,
59    /// A color used to indicate a warning condition.
60    Warning,
61}
62
63impl Color {
64    /// Returns the Color's HSLA value.
65    pub fn color(&self, cx: &App) -> Hsla {
66        match self {
67            Color::Default => cx.theme().colors().text,
68            Color::Muted => cx.theme().colors().text_muted,
69            Color::Created => cx.theme().status().created,
70            Color::Modified => cx.theme().status().modified,
71            Color::Conflict => cx.theme().status().conflict,
72            Color::Ignored => cx.theme().status().ignored,
73            Color::Deleted => cx.theme().status().deleted,
74            Color::Disabled => cx.theme().colors().text_disabled,
75            Color::Hidden => cx.theme().status().hidden,
76            Color::Hint => cx.theme().status().hint,
77            Color::Info => cx.theme().status().info,
78            Color::Placeholder => cx.theme().colors().text_placeholder,
79            Color::Accent => cx.theme().colors().text_accent,
80            Color::Player(i) => cx.theme().styles.player.color_for_participant(*i).cursor,
81            Color::Error => cx.theme().status().error,
82            Color::Selected => cx.theme().colors().text_accent,
83            Color::Success => cx.theme().status().success,
84            Color::Warning => cx.theme().status().warning,
85            Color::Custom(color) => *color,
86        }
87    }
88}
89
90impl From<Hsla> for Color {
91    fn from(color: Hsla) -> Self {
92        Color::Custom(color)
93    }
94}