Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T06:51:53.192Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

color_extractor.rs

156 lines · 4.8 KB · rust
1use gpui::{Hsla, rgba};
2use lsp::{CompletionItem, CompletionItemKind, Documentation};
3use project::color_extractor::*;
4
5pub const COLOR_TABLE: &[(&str, Option<u32>)] = &[
6    // -- Invalid --
7    // Invalid hex
8    ("f0f", None),
9    ("#fof", None),
10    // Extra field
11    ("rgb(255, 0, 0, 0.0)", None),
12    ("hsl(120, 0, 0, 0.0)", None),
13    // Missing field
14    ("rgba(255, 0, 0)", None),
15    ("hsla(120, 0, 0)", None),
16    // No decimal after zero
17    ("rgba(255, 0, 0, 0)", None),
18    ("hsla(120, 0, 0, 0)", None),
19    // Decimal after one
20    ("rgba(255, 0, 0, 1.0)", None),
21    ("hsla(120, 0, 0, 1.0)", None),
22    // HEX (sRGB)
23    ("#f0f", Some(0xFF00FFFF)),
24    ("#ff0000", Some(0xFF0000FF)),
25    // RGB / RGBA (sRGB)
26    ("rgb(255, 0, 0)", Some(0xFF0000FF)),
27    ("rgba(255, 0, 0, 0.4)", Some(0xFF000066)),
28    ("rgba(255, 0, 0, 1)", Some(0xFF0000FF)),
29    ("rgb(20%, 0%, 0%)", Some(0x330000FF)),
30    ("rgba(20%, 0%, 0%, 1)", Some(0x330000FF)),
31    ("rgb(0%, 20%, 0%)", Some(0x003300FF)),
32    ("rgba(0%, 20%, 0%, 1)", Some(0x003300FF)),
33    ("rgb(0%, 0%, 20%)", Some(0x000033FF)),
34    ("rgba(0%, 0%, 20%, 1)", Some(0x000033FF)),
35    // HSL / HSLA (sRGB)
36    ("hsl(0, 100%, 50%)", Some(0xFF0000FF)),
37    ("hsl(120, 100%, 50%)", Some(0x00FF00FF)),
38    ("hsla(0, 100%, 50%, 0.0)", Some(0xFF000000)),
39    ("hsla(0, 100%, 50%, 0.4)", Some(0xFF000066)),
40    ("hsla(0, 100%, 50%, 1)", Some(0xFF0000FF)),
41    ("hsla(120, 100%, 50%, 0.0)", Some(0x00FF0000)),
42    ("hsla(120, 100%, 50%, 0.4)", Some(0x00FF0066)),
43    ("hsla(120, 100%, 50%, 1)", Some(0x00FF00FF)),
44];
45
46#[test]
47fn can_extract_from_label() {
48    for (color_str, color_val) in COLOR_TABLE.iter() {
49        let color = extract_color(&CompletionItem {
50            kind: Some(CompletionItemKind::COLOR),
51            label: color_str.to_string(),
52            detail: None,
53            documentation: None,
54            ..Default::default()
55        });
56
57        assert_eq!(color, color_val.map(|v| Hsla::from(rgba(v))));
58    }
59}
60
61#[test]
62fn only_whole_label_matches_are_allowed() {
63    for (color_str, _) in COLOR_TABLE.iter() {
64        let color = extract_color(&CompletionItem {
65            kind: Some(CompletionItemKind::COLOR),
66            label: format!("{} foo", color_str).to_string(),
67            detail: None,
68            documentation: None,
69            ..Default::default()
70        });
71
72        assert_eq!(color, None);
73    }
74}
75
76#[test]
77fn can_extract_from_detail() {
78    for (color_str, color_val) in COLOR_TABLE.iter() {
79        let color = extract_color(&CompletionItem {
80            kind: Some(CompletionItemKind::COLOR),
81            label: "".to_string(),
82            detail: Some(color_str.to_string()),
83            documentation: None,
84            ..Default::default()
85        });
86
87        assert_eq!(color, color_val.map(|v| Hsla::from(rgba(v))));
88    }
89}
90
91#[test]
92fn only_whole_detail_matches_are_allowed() {
93    for (color_str, _) in COLOR_TABLE.iter() {
94        let color = extract_color(&CompletionItem {
95            kind: Some(CompletionItemKind::COLOR),
96            label: "".to_string(),
97            detail: Some(format!("{} foo", color_str).to_string()),
98            documentation: None,
99            ..Default::default()
100        });
101
102        assert_eq!(color, None);
103    }
104}
105
106#[test]
107fn can_extract_from_documentation_start() {
108    for (color_str, color_val) in COLOR_TABLE.iter() {
109        let color = extract_color(&CompletionItem {
110            kind: Some(CompletionItemKind::COLOR),
111            label: "".to_string(),
112            detail: None,
113            documentation: Some(Documentation::String(
114                format!("{} foo", color_str).to_string(),
115            )),
116            ..Default::default()
117        });
118
119        assert_eq!(color, color_val.map(|v| Hsla::from(rgba(v))));
120    }
121}
122
123#[test]
124fn can_extract_from_documentation_end() {
125    for (color_str, color_val) in COLOR_TABLE.iter() {
126        let color = extract_color(&CompletionItem {
127            kind: Some(CompletionItemKind::COLOR),
128            label: "".to_string(),
129            detail: None,
130            documentation: Some(Documentation::String(
131                format!("foo {}", color_str).to_string(),
132            )),
133            ..Default::default()
134        });
135
136        assert_eq!(color, color_val.map(|v| Hsla::from(rgba(v))));
137    }
138}
139
140#[test]
141fn cannot_extract_from_documentation_middle() {
142    for (color_str, _) in COLOR_TABLE.iter() {
143        let color = extract_color(&CompletionItem {
144            kind: Some(CompletionItemKind::COLOR),
145            label: "".to_string(),
146            detail: None,
147            documentation: Some(Documentation::String(
148                format!("foo {} foo", color_str).to_string(),
149            )),
150            ..Default::default()
151        });
152
153        assert_eq!(color, None);
154    }
155}
156
Served at tenant.openagents/omega Member data and write actions are omitted.