Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
tideui/color.go at main · allisonhere/tideui · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
allisonhere
tideui
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
tideui
/
color.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
310 lines (284 loc) · 7.73 KB
main
Breadcrumbs
tideui
/
color.go
Copy path
Top
File metadata and controls
Code
Blame
310 lines (284 loc) · 7.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package
tideui
import
(
"fmt"
"math"
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
)
// paneFocusMinContrast is the contrast floor for the focused-pane border —
// deliberately higher than the general 4.5 text-readability bar so the focus
// highlight remains the strongest-contrast element in the UI.
const
paneFocusMinContrast
=
7.0
// selectedBgMinContrast is the contrast floor a selected row's background
// must clear against the pane background, so the current selection reads as
// visually distinct rather than a faint tint.
const
selectedBgMinContrast
=
3.0
// workspaceFocusMinContrast is the floor for the focused panel frame in the
// workspace. It is intentionally lower than the base pane's 7:1 so focus reads
// as an accent edge rather than a neon outline on a busy dashboard; content
// stays fully readable because only the frame changes.
const
workspaceFocusMinContrast
=
4.5
// workspaceFocusColor keeps the accent's hue while reaching a readable but not
// neon contrast against the workspace background. It falls back to a
// guaranteed-contrast colour only if the accent cannot get close.
func
workspaceFocusColor
(
accent
,
bg
lipgloss.
Color
) lipgloss.
Color
{
c
:=
AccentReadableOn
(
accent
,
bg
,
5.0
)
if
contrastRatio
(
c
,
bg
)
<
workspaceFocusMinContrast
{
c
=
readableText
(
accent
,
bg
,
workspaceFocusMinContrast
)
}
return
c
}
// shadowBgMinContrast is the contrast floor a modal drop shadow's
// background must clear against the page background it sits on, so the
// shadow reads as a shadow instead of an invisible near-match.
const
shadowBgMinContrast
=
1.6
func
hexToRGB
(
c
lipgloss.
Color
) (
r
,
g
,
b
float64
,
ok
bool
) {
s
:=
strings
.
TrimPrefix
(
string
(
c
),
"#"
)
if
len
(
s
)
!=
6
{
return
}
ri
,
e1
:=
strconv
.
ParseUint
(
s
[
0
:
2
],
16
,
8
)
gi
,
e2
:=
strconv
.
ParseUint
(
s
[
2
:
4
],
16
,
8
)
bi
,
e3
:=
strconv
.
ParseUint
(
s
[
4
:
6
],
16
,
8
)
if
e1
!=
nil
||
e2
!=
nil
||
e3
!=
nil
{
return
}
return
float64
(
ri
)
/
255
,
float64
(
gi
)
/
255
,
float64
(
bi
)
/
255
,
true
}
func
srgbLinearize
(
v
float64
)
float64
{
if
v
<=
0.04045
{
return
v
/
12.92
}
return
math
.
Pow
((
v
+
0.055
)
/
1.055
,
2.4
)
}
func
luminance
(
c
lipgloss.
Color
)
float64
{
r
,
g
,
b
,
ok
:=
hexToRGB
(
c
)
if
!
ok
{
return
0
}
return
0.2126
*
srgbLinearize
(
r
)
+
0.7152
*
srgbLinearize
(
g
)
+
0.0722
*
srgbLinearize
(
b
)
}
func
isDark
(
c
lipgloss.
Color
)
bool
{
return
luminance
(
c
)
<
0.179
}
func
contrastFg
(
bg
lipgloss.
Color
) lipgloss.
Color
{
if
isDark
(
bg
) {
return
lipgloss
.
Color
(
"#ffffff"
)
}
return
lipgloss
.
Color
(
"#000000"
)
}
func
contrastRatio
(
a
,
b
lipgloss.
Color
)
float64
{
la
,
lb
:=
luminance
(
a
),
luminance
(
b
)
if
la
<
lb
{
la
,
lb
=
lb
,
la
}
return
(
la
+
0.05
)
/
(
lb
+
0.05
)
}
func
readableText
(
preferred
,
bg
lipgloss.
Color
,
minimum
float64
) lipgloss.
Color
{
if
preferred
!=
""
&&
contrastRatio
(
preferred
,
bg
)
>=
minimum
{
return
preferred
}
return
contrastFg
(
bg
)
}
// Legible returns c unchanged when it already reaches minimum contrast against
// bg, and otherwise moves its lightness (keeping hue and saturation) away from
// bg just far enough to reach it. Palettes copied from editor themes often use
// accent colors that are fine for syntax but under 4.5:1 as text; this keeps
// their identity while making text roles readable.
func
Legible
(
c
,
bg
lipgloss.
Color
,
minimum
float64
) lipgloss.
Color
{
if
c
==
""
||
contrastRatio
(
c
,
bg
)
>=
minimum
{
return
c
}
step
:=
0.02
if
!
isDark
(
bg
) {
step
=
-
step
}
cur
:=
c
for
range
50
{
cur
=
adjustLightness
(
cur
,
step
)
if
contrastRatio
(
cur
,
bg
)
>=
minimum
{
return
cur
}
}
return
contrastFg
(
bg
)
}
func
modalSurface
(
t
Theme
) lipgloss.
Color
{
if
t
.
Overlay
!=
""
{
return
t
.
Overlay
}
if
isDark
(
t
.
Bg
) {
return
adjustLightness
(
t
.
Bg
,
0.06
)
}
return
adjustLightness
(
t
.
Bg
,
-
0.06
)
}
func
focusLineBg
(
t
Theme
) lipgloss.
Color
{
step
:=
0.04
if
!
isDark
(
t
.
Bg
) {
step
=
-
step
}
cur
:=
t
.
Bg
for
range
16
{
cur
=
adjustLightness
(
cur
,
step
)
if
contrastRatio
(
cur
,
t
.
Bg
)
>=
1.5
{
return
cur
}
}
if
contrastRatio
(
t
.
Selected
,
t
.
Bg
)
>=
1.5
{
return
t
.
Selected
}
return
cur
}
// selectionBgForRatio nudges bg's lightness until it reaches minRatio contrast
// against bg itself, so the selected-row highlight actually stands out instead
// of relying on a fixed delta that goes unnoticeable on some themes (a plain
// lightness bump on an already-light or already-dark background can land well
// under 2:1).
func
selectionBgForRatio
(
bg
lipgloss.
Color
,
minRatio
float64
) lipgloss.
Color
{
const
step
=
0.03
const
maxSteps
=
30
dir
:=
step
if
!
isDark
(
bg
) {
dir
=
-
step
}
cur
:=
bg
for
range
maxSteps
{
next
:=
adjustLightness
(
cur
,
dir
)
if
next
==
cur
{
break
}
cur
=
next
if
contrastRatio
(
cur
,
bg
)
>=
minRatio
{
return
cur
}
}
return
cur
}
// shadowColor darkens bg until it clears shadowBgMinContrast against bg
// itself, always in the darkening direction regardless of theme — unlike
// selectionBgForRatio, a shadow should always read as "darker," not
// "further away," on both light and dark themes.
func
shadowColor
(
bg
lipgloss.
Color
) lipgloss.
Color
{
const
step
=
0.03
const
maxSteps
=
30
cur
:=
bg
for
range
maxSteps
{
next
:=
adjustLightness
(
cur
,
-
step
)
if
next
==
cur
{
break
}
cur
=
next
if
contrastRatio
(
cur
,
bg
)
>=
shadowBgMinContrast
{
return
cur
}
}
return
cur
}
// blendBg lerps a cell's currently-resolved background color toward target
// by alpha (0 = unchanged, 1 = fully target) — the per-cell color math a
// true alpha-blended shadow needs, as opposed to shadowColor's flat
// rectangle. existing is the ansi.Color already resolved for that cell by
// cellbuf (nil means the cell never had an explicit background set, i.e.
// it's showing the page's own default background — page fills in for it).
func
blendBg
(
existing
ansi.
Color
,
page
,
target
lipgloss.
Color
,
alpha
float64
) ansi.
RGBColor
{
var
er
,
eg
,
eb
float64
if
existing
==
nil
{
er
,
eg
,
eb
,
_
=
hexToRGB
(
page
)
}
else
{
r
,
g
,
b
,
_
:=
existing
.
RGBA
()
er
,
eg
,
eb
=
float64
(
r
>>
8
)
/
255
,
float64
(
g
>>
8
)
/
255
,
float64
(
b
>>
8
)
/
255
}
tr
,
tg
,
tb
,
_
:=
hexToRGB
(
target
)
return
ansi.
RGBColor
{
R
:
uint8
(
clamp01
(
er
+
(
tr
-
er
)
*
alpha
)
*
255
+
0.5
),
G
:
uint8
(
clamp01
(
eg
+
(
tg
-
eg
)
*
alpha
)
*
255
+
0.5
),
B
:
uint8
(
clamp01
(
eb
+
(
tb
-
eb
)
*
alpha
)
*
255
+
0.5
),
}
}
func
clamp01
(
v
float64
)
float64
{
if
v
<
0
{
return
0
}
if
v
>
1
{
return
1
}
return
v
}
func
mutedText
(
text
,
bg
lipgloss.
Color
) lipgloss.
Color
{
delta
:=
-
0.20
if
!
isDark
(
bg
) {
delta
=
0.20
}
candidate
:=
adjustLightness
(
text
,
delta
)
if
contrastRatio
(
candidate
,
bg
)
>=
3
{
return
candidate
}
return
adjustLightness
(
text
,
delta
*
0.6
)
}
func
rgbToHSL
(
r
,
g
,
b
float64
) (
h
,
s
,
l
float64
) {
hi
:=
math
.
Max
(
r
,
math
.
Max
(
g
,
b
))
lo
:=
math
.
Min
(
r
,
math
.
Min
(
g
,
b
))
l
=
(
hi
+
lo
)
/
2
if
hi
==
lo
{
return
0
,
0
,
l
}
d
:=
hi
-
lo
if
l
>
0.5
{
s
=
d
/
(
2
-
hi
-
lo
)
}
else
{
s
=
d
/
(
hi
+
lo
)
}
switch
hi
{
case
r
:
h
=
(
g
-
b
)
/
d
if
g
<
b
{
h
+=
6
}
case
g
:
h
=
(
b
-
r
)
/
d
+
2
case
b
:
h
=
(
r
-
g
)
/
d
+
4
}
return
h
/
6
,
s
,
l
}
func
hue2rgb
(
p
,
q
,
t
float64
)
float64
{
if
t
<
0
{
t
++
}
if
t
>
1
{
t
--
}
switch
{
case
t
<
1.0
/
6
:
return
p
+
(
q
-
p
)
*
6
*
t
case
t
<
0.5
:
return
q
case
t
<
2.0
/
3
:
return
p
+
(
q
-
p
)
*
(
2.0
/
3
-
t
)
*
6
default
:
return
p
}
}
func
hslToRGB
(
h
,
s
,
l
float64
) (
r
,
g
,
b
float64
) {
if
s
==
0
{
return
l
,
l
,
l
}
var
q
float64
if
l
<
0.5
{
q
=
l
*
(
1
+
s
)
}
else
{
q
=
l
+
s
-
l
*
s
}
p
:=
2
*
l
-
q
return
hue2rgb
(
p
,
q
,
h
+
1.0
/
3
),
hue2rgb
(
p
,
q
,
h
),
hue2rgb
(
p
,
q
,
h
-
1.0
/
3
)
}
func
adjustLightness
(
c
lipgloss.
Color
,
delta
float64
) lipgloss.
Color
{
r
,
g
,
b
,
ok
:=
hexToRGB
(
c
)
if
!
ok
{
return
c
}
h
,
s
,
l
:=
rgbToHSL
(
r
,
g
,
b
)
l
=
math
.
Max
(
0
,
math
.
Min
(
1
,
l
+
delta
))
nr
,
ng
,
nb
:=
hslToRGB
(
h
,
s
,
l
)
return
lipgloss
.
Color
(
fmt
.
Sprintf
(
"#%02x%02x%02x"
,
uint8
(
math
.
Round
(
nr
*
255
)),
uint8
(
math
.
Round
(
ng
*
255
)),
uint8
(
math
.
Round
(
nb
*
255
))))
}
You can’t perform that action at this time.