Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
de9bf5bb
H
hub
项目概览
github
/
hub
11 个月 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
de9bf5bb
编写于
12月 22, 2018
作者:
A
Andrew Janke
提交者:
Mislav Marohnić
12月 28, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve contrast ratio for label text coloring
上级
8ba932b6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
137 addition
and
6 deletion
+137
-6
commands/issue.go
commands/issue.go
+37
-6
utils/color.go
utils/color.go
+100
-0
未找到文件。
commands/issue.go
浏览文件 @
de9bf5bb
...
...
@@ -600,13 +600,44 @@ func formatLabel(label github.IssueLabel, colorize bool) string {
func
colorizeLabel
(
label
github
.
IssueLabel
,
color
*
utils
.
Color
)
string
{
bgColorCode
:=
utils
.
RgbToTermColorCode
(
color
)
return
fmt
.
Sprintf
(
"
\0
33[38;5;%d;48;%sm %s
\0
33[m"
,
getSuitableLabelTextColor
(
color
),
bgColorCode
,
label
.
Name
)
fgColor
:=
pickHighContrastTextColor
(
color
)
fgColorCode
:=
utils
.
RgbToTermColorCode
(
fgColor
)
return
fmt
.
Sprintf
(
"
\0
33[38;%s;48;%sm %s
\0
33[m"
,
fgColorCode
,
bgColorCode
,
label
.
Name
)
}
func
getSuitableLabelTextColor
(
color
*
utils
.
Color
)
int
{
if
color
.
Brightness
()
<
0.65
{
return
15
// white text
func
pickHighContrastTextColor
(
color
*
utils
.
Color
)
*
utils
.
Color
{
var
candidates
[
12
]
*
utils
.
Color
hsl
:=
color
.
ToHsl
()
a
:=
hsl
.
ScaleLightness
(
0.5
)
candidates
[
0
]
=
a
.
ToRgb
()
candidates
[
1
]
=
a
.
ScaleLightness
(
0.25
)
.
ToRgb
()
candidates
[
2
]
=
a
.
ScaleSaturation
(
-
0.25
)
.
ToRgb
()
b
:=
hsl
.
ScaleSaturation
(
0.75
)
candidates
[
3
]
=
b
.
ToRgb
()
candidates
[
4
]
=
b
.
ScaleLightness
(
0.25
)
.
ToRgb
()
candidates
[
5
]
=
b
.
ScaleSaturation
(
-
0.25
)
.
ToRgb
()
c
:=
hsl
.
ScaleLightness
(
-
0.5
)
candidates
[
6
]
=
c
.
ToRgb
()
candidates
[
7
]
=
c
.
ScaleLightness
(
-
0.25
)
.
ToRgb
()
candidates
[
8
]
=
c
.
ScaleSaturation
(
-
0.25
)
.
ToRgb
()
d
:=
hsl
.
ScaleSaturation
(
-
0.75
)
candidates
[
9
]
=
d
.
ToRgb
()
candidates
[
10
]
=
d
.
ScaleLightness
(
-
0.25
)
.
ToRgb
()
candidates
[
11
]
=
d
.
ScaleSaturation
(
-
0.25
)
.
ToRgb
()
foundContrastRatio
:=
-
999.0
ix
:=
-
1
for
i
:=
0
;
i
<
12
;
i
++
{
contrastRatio
:=
color
.
ContrastRatio
(
candidates
[
i
])
if
contrastRatio
>
foundContrastRatio
{
ix
=
i
foundContrastRatio
=
contrastRatio
}
}
if
foundContrastRatio
>=
7.0
{
return
candidates
[
ix
]
}
else
{
return
candidates
[
11
]
}
return
16
// black text
}
utils/color.go
浏览文件 @
de9bf5bb
...
...
@@ -50,6 +50,106 @@ func (c *Color) Distance(other *Color) float64 {
math
.
Pow
(
float64
(
c
.
Blue
-
other
.
Blue
),
2
)))
}
func
rgbComponentToBoldValue
(
component
int64
)
float64
{
srgb
:=
float64
(
component
)
/
255
if
srgb
<=
0.03928
{
return
srgb
/
12.92
}
else
{
return
math
.
Pow
(((
srgb
+
0.055
)
/
1.055
),
2.4
)
}
}
func
(
c
*
Color
)
Luminance
()
float64
{
return
0.2126
*
rgbComponentToBoldValue
(
c
.
Red
)
+
0.7152
*
rgbComponentToBoldValue
(
c
.
Green
)
+
0.0722
*
rgbComponentToBoldValue
(
c
.
Blue
)
}
func
(
c
*
Color
)
ContrastRatio
(
other
*
Color
)
float64
{
L
:=
c
.
Luminance
()
otherL
:=
other
.
Luminance
()
var
L1
,
L2
float64
if
L
>
otherL
{
L1
,
L2
=
L
,
otherL
}
else
{
L1
,
L2
=
otherL
,
L
}
ratio
:=
(
L1
+
0.05
)
/
(
L2
+
0.05
)
return
ratio
}
type
HslColor
struct
{
Hue
float64
Saturation
float64
Lightness
float64
}
func
(
c
*
Color
)
ToHsl
()
*
HslColor
{
rPrime
:=
float64
(
c
.
Red
)
/
255
gPrime
:=
float64
(
c
.
Green
)
/
255
bPrime
:=
float64
(
c
.
Blue
)
/
255
cMax
:=
math
.
Max
(
rPrime
,
math
.
Max
(
gPrime
,
bPrime
))
cMin
:=
math
.
Min
(
rPrime
,
math
.
Min
(
gPrime
,
bPrime
))
delta
:=
cMax
-
cMin
var
H
float64
if
delta
==
0
{
H
=
0
}
else
if
cMax
==
rPrime
{
H
=
60
*
math
.
Mod
((
gPrime
-
bPrime
)
/
delta
,
6
)
}
else
if
cMax
==
gPrime
{
H
=
60
*
(((
bPrime
-
rPrime
)
/
delta
)
+
2
)
}
else
{
H
=
60
*
(((
rPrime
-
gPrime
)
/
delta
)
+
4
)
}
var
L
float64
L
=
(
cMax
+
cMin
)
/
2
var
S
float64
if
delta
==
0
{
S
=
0
}
else
{
S
=
delta
/
(
1
-
math
.
Abs
((
2
*
L
)
-
1
))
}
return
&
HslColor
{
H
,
S
,
L
}
}
func
(
c
*
HslColor
)
ToRgb
()
*
Color
{
C
:=
(
1
-
math
.
Abs
(
2
*
c
.
Lightness
-
1
))
*
c
.
Saturation
X
:=
C
*
(
1
-
math
.
Abs
(
math
.
Mod
(
c
.
Hue
/
60
,
2
)
-
1
))
m
:=
c
.
Lightness
-
(
C
/
2
)
var
rPrime
,
gPrime
,
bPrime
float64
switch
{
case
c
.
Hue
<
60.0
:
rPrime
,
gPrime
,
bPrime
=
C
,
X
,
0
case
c
.
Hue
<
120.0
:
rPrime
,
gPrime
,
bPrime
=
X
,
C
,
0
case
c
.
Hue
<
180.0
:
rPrime
,
gPrime
,
bPrime
=
0
,
C
,
X
case
c
.
Hue
<
240.0
:
rPrime
,
gPrime
,
bPrime
=
0
,
X
,
C
case
c
.
Hue
<
300.0
:
rPrime
,
gPrime
,
bPrime
=
X
,
0
,
C
case
c
.
Hue
<
360.0
:
rPrime
,
gPrime
,
bPrime
=
C
,
0
,
X
}
R
:=
int64
((
rPrime
+
m
)
*
255
)
G
:=
int64
((
gPrime
+
m
)
*
255
)
B
:=
int64
((
bPrime
+
m
)
*
255
)
return
&
Color
{
R
,
G
,
B
}
}
func
(
c
*
HslColor
)
ScaleLightness
(
x
float64
)
*
HslColor
{
newLightness
:=
math
.
Round
(
math
.
Min
(
math
.
Max
(
0
,
c
.
Lightness
*
(
1.0
+
x
)),
1.0
))
return
&
HslColor
{
c
.
Hue
,
c
.
Saturation
,
newLightness
}
}
func
(
c
*
HslColor
)
ScaleSaturation
(
x
float64
)
*
HslColor
{
newSaturation
:=
math
.
Round
(
math
.
Min
(
math
.
Max
(
0
,
c
.
Lightness
*
(
1.0
+
x
)),
1.0
))
return
&
HslColor
{
c
.
Hue
,
newSaturation
,
c
.
Lightness
}
}
var
x6colorIndexes
=
[
6
]
int64
{
0
,
95
,
135
,
175
,
215
,
255
}
var
x6colorCube
[
216
]
Color
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录