@@ -20,6 +20,7 @@ import (
20
20
"code.gitea.io/gitea/modules/markup"
21
21
"code.gitea.io/gitea/modules/markup/markdown"
22
22
"code.gitea.io/gitea/modules/setting"
23
+ "code.gitea.io/gitea/modules/util"
23
24
)
24
25
25
26
// RenderCommitMessage renders commit message with XSS-safe and special links.
@@ -133,7 +134,9 @@ func RenderLabel(ctx context.Context, label *issues_model.Label) template.HTML {
133
134
labelScope := label .ExclusiveScope ()
134
135
135
136
textColor := "#111"
136
- if label .UseLightTextColor () {
137
+ r , g , b := util .HexToRBGColor (label .Color )
138
+ // Determine if label text should be light or dark to be readable on background color
139
+ if util .UseLightTextOnBackground (r , g , b ) {
137
140
textColor = "#eee"
138
141
}
139
142
@@ -150,34 +153,30 @@ func RenderLabel(ctx context.Context, label *issues_model.Label) template.HTML {
150
153
scopeText := RenderEmoji (ctx , labelScope )
151
154
itemText := RenderEmoji (ctx , label .Name [len (labelScope )+ 1 :])
152
155
153
- itemColor := label .Color
154
- scopeColor := label .Color
155
- if r , g , b , err := label .ColorRGB (); err == nil {
156
- // Make scope and item background colors slightly darker and lighter respectively.
157
- // More contrast needed with higher luminance, empirically tweaked.
158
- luminance := (0.299 * r + 0.587 * g + 0.114 * b ) / 255
159
- contrast := 0.01 + luminance * 0.03
160
- // Ensure we add the same amount of contrast also near 0 and 1.
161
- darken := contrast + math .Max (luminance + contrast - 1.0 , 0.0 )
162
- lighten := contrast + math .Max (contrast - luminance , 0.0 )
163
- // Compute factor to keep RGB values proportional.
164
- darkenFactor := math .Max (luminance - darken , 0.0 ) / math .Max (luminance , 1.0 / 255.0 )
165
- lightenFactor := math .Min (luminance + lighten , 1.0 ) / math .Max (luminance , 1.0 / 255.0 )
166
-
167
- scopeBytes := []byte {
168
- uint8 (math .Min (math .Round (r * darkenFactor ), 255 )),
169
- uint8 (math .Min (math .Round (g * darkenFactor ), 255 )),
170
- uint8 (math .Min (math .Round (b * darkenFactor ), 255 )),
171
- }
172
- itemBytes := []byte {
173
- uint8 (math .Min (math .Round (r * lightenFactor ), 255 )),
174
- uint8 (math .Min (math .Round (g * lightenFactor ), 255 )),
175
- uint8 (math .Min (math .Round (b * lightenFactor ), 255 )),
176
- }
177
-
178
- itemColor = "#" + hex .EncodeToString (itemBytes )
179
- scopeColor = "#" + hex .EncodeToString (scopeBytes )
180
- }
156
+ // Make scope and item background colors slightly darker and lighter respectively.
157
+ // More contrast needed with higher luminance, empirically tweaked.
158
+ luminance := util .GetLuminance (r , g , b )
159
+ contrast := 0.01 + luminance * 0.03
160
+ // Ensure we add the same amount of contrast also near 0 and 1.
161
+ darken := contrast + math .Max (luminance + contrast - 1.0 , 0.0 )
162
+ lighten := contrast + math .Max (contrast - luminance , 0.0 )
163
+ // Compute factor to keep RGB values proportional.
164
+ darkenFactor := math .Max (luminance - darken , 0.0 ) / math .Max (luminance , 1.0 / 255.0 )
165
+ lightenFactor := math .Min (luminance + lighten , 1.0 ) / math .Max (luminance , 1.0 / 255.0 )
166
+
167
+ scopeBytes := []byte {
168
+ uint8 (math .Min (math .Round (r * darkenFactor ), 255 )),
169
+ uint8 (math .Min (math .Round (g * darkenFactor ), 255 )),
170
+ uint8 (math .Min (math .Round (b * darkenFactor ), 255 )),
171
+ }
172
+ itemBytes := []byte {
173
+ uint8 (math .Min (math .Round (r * lightenFactor ), 255 )),
174
+ uint8 (math .Min (math .Round (g * lightenFactor ), 255 )),
175
+ uint8 (math .Min (math .Round (b * lightenFactor ), 255 )),
176
+ }
177
+
178
+ itemColor := "#" + hex .EncodeToString (itemBytes )
179
+ scopeColor := "#" + hex .EncodeToString (scopeBytes )
181
180
182
181
s := fmt .Sprintf ("<span class='ui label scope-parent' title='%s'>" +
183
182
"<div class='ui label scope-left' style='color: %s !important; background-color: %s !important'>%s</div>" +
0 commit comments