Skip to content

Commit c13f8c7

Browse files
committed
unify frontend and backend
1 parent 32e381e commit c13f8c7

File tree

4 files changed

+28
-41
lines changed

4 files changed

+28
-41
lines changed

modules/util/color.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ func getLuminanceRGB(channel float64) float64 {
2121
}
2222

2323
// Get color as RGB values in 0..255 range from the hex color string (with or without #)
24-
// TODO: support return of rgba
2524
func HexToRBGColor(colorString string) (float64, float64, float64) {
2625
hexString := colorString
2726
if strings.HasPrefix(colorString, "#") {
28-
hexString = hexString[1:]
27+
hexString = colorString[1:]
2928
}
30-
// only support transfer of rgb and rrggbb
31-
// if not in this format, use default values 0, 0, 0
32-
if len(hexString) != 3 && len(hexString) != 6 {
29+
// only support transfer of rgb, rgba, rrggbb and rrggbbaa
30+
// if not in these formats, use default values 0, 0, 0
31+
fmt.Println(hexString)
32+
if len(hexString) != 3 && len(hexString) != 4 && len(hexString) != 6 && len(hexString) != 8 {
3333
return 0, 0, 0
3434
}
35-
if len(hexString) == 3 {
35+
if len(hexString) == 3 || len(hexString) == 4 {
3636
hexString = fmt.Sprintf("%c%c%c%c%c%c", hexString[0], hexString[0], hexString[1], hexString[1], hexString[2], hexString[2])
3737
}
38+
if len(hexString) == 8 {
39+
hexString = hexString[0:6]
40+
}
3841
color, err := strconv.ParseUint(hexString, 16, 64)
3942
if err != nil {
4043
return 0, 0, 0

modules/util/color_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ func Test_HexToRBGColor(t *testing.T) {
1818
{"2b8685", 43, 134, 133},
1919
{"1e1", 17, 238, 17},
2020
{"#1e1", 17, 238, 17},
21-
{"3bb6", 0, 0, 0},
22-
{"3bb64", 0, 0, 0},
23-
{"#7e716c", 126, 113, 108},
24-
{"#1e11", 0, 0, 0},
25-
{"#3bb64", 0, 0, 0},
26-
{"#2e2", 34, 238, 34},
27-
{"2e2", 34, 238, 34},
21+
{"1e16", 17, 238, 17},
22+
{"3bb6b3", 59, 182, 179},
23+
{"#3bb6b399", 59, 182, 179},
24+
{"#0", 0, 0, 0},
25+
{"#00000", 0, 0, 0},
26+
{"#1234567", 0, 0, 0},
2827
}
2928
for n, c := range cases {
3029
r, g, b := HexToRBGColor(c.colorString)

web_src/js/utils/color.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,25 @@ function getLuminance(r, g, b) {
1313
return 0.2126 * R + 0.7152 * G + 0.0722 * B;
1414
}
1515

16-
const convertHexUnitTo256 = (hexStr) => parseInt(hexStr.repeat(2 / hexStr.length), 16);
17-
18-
function getAlphafloat(alpha) {
19-
if (alpha !== undefined) {
20-
return alpha / 255;
21-
}
22-
return 1;
23-
}
24-
25-
const re = /.{1,2}/g;
2616
// Get color as RGB values in 0..255 range from the hex color string (with or without #)
27-
export function hexToRGBColor(backgroundColorStr, ignoreAlpha = true) {
17+
export function hexToRGBColor(backgroundColorStr) {
2818
let backgroundColor = backgroundColorStr;
2919
if (backgroundColorStr[0] === '#') {
3020
backgroundColor = backgroundColorStr.substring(1);
3121
}
32-
// only support transfer of rgb, rgba, rrggbb, and rrggbbaa
33-
// if not in this format, use default values 0, 0, 0 or 0, 0, 0, 1
22+
// only support transfer of rgb, rgba, rrggbb and rrggbbaa
23+
// if not in these formats, use default values 0, 0, 0
3424
if (![3, 4, 6, 8].includes(backgroundColor.length)) {
35-
return ignoreAlpha ? [0, 0, 0] : [0, 0, 0, 1];
25+
return [0, 0, 0];
26+
}
27+
if ([3, 4].includes(backgroundColor.length)) {
28+
const [r, g, b] = backgroundColor;
29+
backgroundColor = `${r}${r}${g}${g}${b}${b}`;
3630
}
37-
// chunkSize is number of digits that should be grouped together to form a RGBA channel
38-
const chunkSize = Math.floor(backgroundColor.length / 3);
39-
// hexArr is array of [r, g, b] or [r, g, b, a], a could be undefined
40-
// and will be processed in getAlphafloat if ignoreAlpha is false
41-
const hexArr = chunkSize === 1 ? backgroundColor.split('') : backgroundColor.match(re);
42-
const [r, g, b, a] = hexArr.map(convertHexUnitTo256);
43-
return ignoreAlpha ? [r, g, b] : [r, g, b, getAlphafloat(a)];
31+
const r = parseInt(backgroundColor.substring(0, 2), 16);
32+
const g = parseInt(backgroundColor.substring(2, 4), 16);
33+
const b = parseInt(backgroundColor.substring(4, 6), 16);
34+
return [r, g, b];
4435
}
4536

4637
// Reference from: https://firsching.ch/github_labels.html

web_src/js/utils/color.test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ test('hexToRGBColor', () => {
55
expect(hexToRGBColor('2b8685')).toEqual([43, 134, 133]);
66
expect(hexToRGBColor('1e1')).toEqual([17, 238, 17]);
77
expect(hexToRGBColor('#1e1')).toEqual([17, 238, 17]);
8-
expect(hexToRGBColor('#1e1', false)).toEqual([17, 238, 17, 1]);
98
expect(hexToRGBColor('1e16')).toEqual([17, 238, 17]);
10-
expect(hexToRGBColor('1e16', false)).toEqual([17, 238, 17, 0.4]);
11-
expect(hexToRGBColor('#1e16', false)).toEqual([17, 238, 17, 0.4]);
129
expect(hexToRGBColor('3bb6b3')).toEqual([59, 182, 179]);
13-
expect(hexToRGBColor('3bb6b399', false)).toEqual([59, 182, 179, 0.6]);
1410
expect(hexToRGBColor('#3bb6b399')).toEqual([59, 182, 179]);
15-
expect(hexToRGBColor('#807070', false)).toEqual([128, 112, 112, 1]);
1611
expect(hexToRGBColor('#0')).toEqual([0, 0, 0]);
17-
expect(hexToRGBColor('#00', false)).toEqual([0, 0, 0, 1]);
1812
expect(hexToRGBColor('#00000')).toEqual([0, 0, 0]);
19-
expect(hexToRGBColor('#0000000', false)).toEqual([0, 0, 0, 1]);
13+
expect(hexToRGBColor('#1234567')).toEqual([0, 0, 0]);
2014
});
2115

2216
test('useLightTextOnBackground', () => {

0 commit comments

Comments
 (0)