-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Modify luminance calculation and extract related functions into single files #24586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
silverwind
merged 48 commits into
go-gitea:main
from
HesterG:modify-lightness-calculation
May 10, 2023
Merged
Changes from 27 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
17ecae7
change lightness calculation function
HesterG 8ab875e
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG ad2b5e7
save changes
HesterG 22c507f
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG d2ffc63
add some tests
HesterG 40ef2e2
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG 29c5a8d
add more tests
HesterG 37933b4
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG 4d3ed67
use same logic as github and modify tests
HesterG 75f1f66
remove unused
HesterG 5f1aa4d
fix
HesterG 1efcd07
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG c1fd625
modify tests
HesterG b6aab0c
save changes and add tests
HesterG d437286
save changes
HesterG dc60ea7
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG 550dde9
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG 21fa83b
modify tests
HesterG 61e7d42
rename function
HesterG 51f5419
adjust functions
HesterG 690d6d4
rename functions
HesterG 9433b8d
adjust functions and tests
HesterG cfb0f00
fix lint
HesterG 9bc8c9f
comment
HesterG ed4c615
Merge branch 'main' into modify-lightness-calculation
HesterG 1182b08
Merge branch 'main' into modify-lightness-calculation
HesterG 8394fa9
Merge branch 'go-gitea:main' into modify-lightness-calculation
HesterG 8dce9d4
save change for js
HesterG 6f109da
modify backend
HesterG 1b9395a
fix lint
HesterG 1c337dd
add commments
HesterG d427c9d
Merge branch 'main' into modify-lightness-calculation
HesterG 745eb19
update hexstring
HesterG cb6790e
remove err return for HexToRBGColor and fix lint
HesterG 9dd72c4
fix lint
HesterG 520931a
try improve regexp
HesterG efa389b
fix lint
HesterG 87581ce
update re
HesterG 524bec8
Merge branch 'main' into modify-lightness-calculation
HesterG 1c27944
Update web_src/js/utils/color.js
HesterG bebecd7
Update web_src/js/utils/color.js
HesterG 9757085
Update web_src/js/utils/color.js
HesterG 32e381e
Merge branch 'main' into modify-lightness-calculation
HesterG c13f8c7
unify frontend and backend
HesterG 02d7036
remove print
HesterG 5ae6973
Merge branch 'main' into modify-lightness-calculation
GiteaBot ac49366
Merge branch 'main' into modify-lightness-calculation
GiteaBot 0a26397
Merge branch 'main' into modify-lightness-calculation
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
package util | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Check similar implementation in web_src/js/utils/color.js and keep synchronization | ||
|
||
// Return R, G, B values defined in reletive luminance | ||
func getLuminanceRGB(channel float64) float64 { | ||
sRGB := channel / 255 | ||
if sRGB <= 0.03928 { | ||
return sRGB / 12.92 | ||
} | ||
return math.Pow((sRGB+0.055)/1.055, 2.4) | ||
} | ||
|
||
// Get color as RGB values in 0..255 range from the hex color string (with or without #) | ||
func HexToRBGColor(colorString string) (float64, float64, float64, error) { | ||
var color uint64 | ||
var err error | ||
if strings.HasPrefix(colorString, "#") { | ||
color, err = strconv.ParseUint(colorString[1:], 16, 64) | ||
} else { | ||
color, err = strconv.ParseUint(colorString, 16, 64) | ||
} | ||
if err != nil { | ||
return 0, 0, 0, err | ||
HesterG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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 | ||
} | ||
|
||
// return luminance given RGB channels | ||
// Reference from: https://www.w3.org/WAI/GL/wiki/Relative_luminance | ||
func GetLuminance(r, g, b float64) float64 { | ||
R := getLuminanceRGB(r) | ||
G := getLuminanceRGB(g) | ||
B := getLuminanceRGB(b) | ||
luminance := 0.2126*R + 0.7152*G + 0.0722*B | ||
return luminance | ||
} | ||
|
||
// Reference from: https://firsching.ch/github_labels.html | ||
// In the future WCAG 3 APCA may be a better solution. | ||
// Check if text should use light color based on RGB of background | ||
func UseLightTextOnBackground(r, g, b float64) bool { | ||
return GetLuminance(r, g, b) < 0.453 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_HexToRBGColor(t *testing.T) { | ||
cases := []struct { | ||
colorString string | ||
expectedR float64 | ||
expectedG float64 | ||
expectedB float64 | ||
}{ | ||
{"2b8685", 43, 134, 133}, | ||
{"2b8786", 43, 135, 134}, | ||
{"2c8786", 44, 135, 134}, | ||
{"3bb6b3", 59, 182, 179}, | ||
{"7c7268", 124, 114, 104}, | ||
{"#7e716c", 126, 113, 108}, | ||
{"#807070", 128, 112, 112}, | ||
{"#81706d", 129, 112, 109}, | ||
{"#d73a4a", 215, 58, 74}, | ||
{"#0075ca", 0, 117, 202}, | ||
} | ||
for n, c := range cases { | ||
r, g, b, _ := HexToRBGColor(c.colorString) | ||
assert.Equal(t, c.expectedR, r, "case %d: error R should match: expected %f, but get %f", n, c.expectedR, r) | ||
assert.Equal(t, c.expectedG, g, "case %d: error G should match: expected %f, but get %f", n, c.expectedG, g) | ||
assert.Equal(t, c.expectedB, b, "case %d: error B should match: expected %f, but get %f", n, c.expectedB, b) | ||
} | ||
} | ||
|
||
func Test_UseLightTextOnBackground(t *testing.T) { | ||
cases := []struct { | ||
r float64 | ||
g float64 | ||
b float64 | ||
expected bool | ||
}{ | ||
{215, 58, 74, true}, | ||
{0, 117, 202, true}, | ||
{207, 211, 215, false}, | ||
{162, 238, 239, false}, | ||
{112, 87, 255, true}, | ||
{0, 134, 114, true}, | ||
{228, 230, 105, false}, | ||
{216, 118, 227, true}, | ||
{255, 255, 255, false}, | ||
{43, 134, 133, true}, | ||
{43, 135, 134, true}, | ||
{44, 135, 134, true}, | ||
{59, 182, 179, true}, | ||
{124, 114, 104, true}, | ||
{126, 113, 108, true}, | ||
{129, 112, 109, true}, | ||
{128, 112, 112, true}, | ||
} | ||
for n, c := range cases { | ||
result := UseLightTextOnBackground(c.r, c.g, c.b) | ||
assert.Equal(t, c.expected, result, "case %d: error should match", n) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Check similar implementation in modules/util/color.go and keep synchronization | ||
// Return R, G, B values defined in reletive luminance | ||
function getLuminanceRGB(channel) { | ||
const sRGB = channel / 255; | ||
const res = (sRGB <= 0.03928) ? sRGB / 12.92 : ((sRGB + 0.055) / 1.055) ** 2.4; | ||
return res; | ||
HesterG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Reference from: https://www.w3.org/WAI/GL/wiki/Relative_luminance | ||
function getLuminance(r, g, b) { | ||
const R = getLuminanceRGB(r); | ||
const G = getLuminanceRGB(g); | ||
const B = getLuminanceRGB(b); | ||
const luminance = 0.2126 * R + 0.7152 * G + 0.0722 * B; | ||
return luminance; | ||
HesterG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Get color as RGB values in 0..255 range from the hex color string (with or without #) | ||
export function hexToRGBColor(backgroundColorStr) { | ||
let backgroundColor = backgroundColorStr; | ||
if (backgroundColorStr[0] === '#') { | ||
backgroundColor = backgroundColorStr.substring(1); | ||
} | ||
const r = parseInt(backgroundColor.substring(0, 2), 16); | ||
const g = parseInt(backgroundColor.substring(2, 4), 16); | ||
const b = parseInt(backgroundColor.substring(4, 6), 16); | ||
return [r, g, b]; | ||
HesterG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Reference from: https://firsching.ch/github_labels.html | ||
// In the future WCAG 3 APCA may be a better solution. | ||
// Check if text should use light color based on RGB of background | ||
export function useLightTextOnBackground(r, g, b) { | ||
return getLuminance(r, g, b) < 0.453; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {test, expect} from 'vitest'; | ||
import {hexToRGBColor, useLightTextOnBackground} from './color.js'; | ||
|
||
test('hexToRGBColor', () => { | ||
expect(hexToRGBColor('2b8685')).toEqual([43, 134, 133]); | ||
expect(hexToRGBColor('2b8786')).toEqual([43, 135, 134]); | ||
expect(hexToRGBColor('2c8786')).toEqual([44, 135, 134]); | ||
expect(hexToRGBColor('3bb6b3')).toEqual([59, 182, 179]); | ||
expect(hexToRGBColor('7c7268')).toEqual([124, 114, 104]); | ||
expect(hexToRGBColor('#7e716c')).toEqual([126, 113, 108]); | ||
expect(hexToRGBColor('#807070')).toEqual([128, 112, 112]); | ||
expect(hexToRGBColor('#81706d')).toEqual([129, 112, 109]); | ||
expect(hexToRGBColor('#d73a4a')).toEqual([215, 58, 74]); | ||
expect(hexToRGBColor('#0075ca')).toEqual([0, 117, 202]); | ||
}); | ||
|
||
test('useLightTextOnBackground', () => { | ||
expect(useLightTextOnBackground(215, 58, 74)).toBe(true); | ||
expect(useLightTextOnBackground(0, 117, 202)).toBe(true); | ||
expect(useLightTextOnBackground(207, 211, 215)).toBe(false); | ||
expect(useLightTextOnBackground(162, 238, 239)).toBe(false); | ||
expect(useLightTextOnBackground(112, 87, 255)).toBe(true); | ||
expect(useLightTextOnBackground(0, 134, 114)).toBe(true); | ||
expect(useLightTextOnBackground(228, 230, 105)).toBe(false); | ||
expect(useLightTextOnBackground(216, 118, 227)).toBe(true); | ||
expect(useLightTextOnBackground(255, 255, 255)).toBe(false); | ||
expect(useLightTextOnBackground(43, 134, 133)).toBe(true); | ||
expect(useLightTextOnBackground(43, 135, 134)).toBe(true); | ||
expect(useLightTextOnBackground(44, 135, 134)).toBe(true); | ||
expect(useLightTextOnBackground(59, 182, 179)).toBe(true); | ||
expect(useLightTextOnBackground(124, 114, 104)).toBe(true); | ||
expect(useLightTextOnBackground(126, 113, 108)).toBe(true); | ||
expect(useLightTextOnBackground(129, 112, 109)).toBe(true); | ||
expect(useLightTextOnBackground(128, 112, 112)).toBe(true); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.