diff options
author | Hester Gong <hestergong@gmail.com> | 2023-05-10 19:19:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-10 11:19:03 +0000 |
commit | ea7954f069bf8bcb87d520f8aab0a80b0768590d (patch) | |
tree | f5edc566c9dbafa3507ed8ef3ffaa32944f80797 /models/issues | |
parent | 0ca5adee16a9de0fb0bd410aa841eeeda3372e23 (diff) | |
download | gitea-ea7954f069bf8bcb87d520f8aab0a80b0768590d.tar.gz gitea-ea7954f069bf8bcb87d520f8aab0a80b0768590d.zip |
Modify luminance calculation and extract related functions into single files (#24586)
Close #24508
Main changes:
As discussed in the issue
1. Change luminance calculation function to use [Relative
Luminance](https://www.w3.org/WAI/GL/wiki/Relative_luminance)
2. Move the luminance related functions into color.go/color.js
3. Add tests for both the files (Not sure if test cases are too many
now)
Before (tests included by `UseLightTextOnBackground` are labels started
with `##`):
https://try.gitea.io/HesterG/testrepo/labels
After:
<img width="1307" alt="Screen Shot 2023-05-08 at 13 37 55"
src="https://user-images.githubusercontent.com/17645053/236742562-fdfc3a4d-2fab-466b-9613-96f2bf96b4bc.png">
<img width="1289" alt="Screen Shot 2023-05-08 at 13 38 06"
src="https://user-images.githubusercontent.com/17645053/236742570-022db68e-cec0-43bb-888a-fc54f5332cc3.png">
<img width="1299" alt="Screen Shot 2023-05-08 at 13 38 20"
src="https://user-images.githubusercontent.com/17645053/236742572-9af1de45-fb7f-460b-828d-ba25fae20f51.png">
---------
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'models/issues')
-rw-r--r-- | models/issues/label.go | 27 | ||||
-rw-r--r-- | models/issues/label_test.go | 9 |
2 files changed, 0 insertions, 36 deletions
diff --git a/models/issues/label.go b/models/issues/label.go index 35c649e8f2..9c22dcdd2d 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -159,33 +159,6 @@ func (l *Label) BelongsToRepo() bool { return l.RepoID > 0 } -// Get color as RGB values in 0..255 range -func (l *Label) ColorRGB() (float64, float64, float64, error) { - color, err := strconv.ParseUint(l.Color[1:], 16, 64) - if err != nil { - return 0, 0, 0, err - } - - r := float64(uint8(0xFF & (uint32(color) >> 16))) - g := float64(uint8(0xFF & (uint32(color) >> 8))) - b := float64(uint8(0xFF & uint32(color))) - return r, g, b, nil -} - -// Determine if label text should be light or dark to be readable on background color -func (l *Label) UseLightTextColor() bool { - if strings.HasPrefix(l.Color, "#") { - if r, g, b, err := l.ColorRGB(); err == nil { - // Perceived brightness from: https://www.w3.org/TR/AERT/#color-contrast - // In the future WCAG 3 APCA may be a better solution - brightness := (0.299*r + 0.587*g + 0.114*b) / 255 - return brightness < 0.35 - } - } - - return false -} - // Return scope substring of label name, or empty string if none exists func (l *Label) ExclusiveScope() string { if !l.Exclusive { diff --git a/models/issues/label_test.go b/models/issues/label_test.go index 1f6ce4f42e..1bc5a1a935 100644 --- a/models/issues/label_test.go +++ b/models/issues/label_test.go @@ -22,15 +22,6 @@ func TestLabel_CalOpenIssues(t *testing.T) { assert.EqualValues(t, 2, label.NumOpenIssues) } -func TestLabel_TextColor(t *testing.T) { - assert.NoError(t, unittest.PrepareTestDatabase()) - label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1}) - assert.False(t, label.UseLightTextColor()) - - label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 2}) - assert.True(t, label.UseLightTextColor()) -} - func TestLabel_ExclusiveScope(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 7}) |