feat: support detect color dark or light
This commit is contained in:
parent
a42d0727df
commit
55eca7b0b9
6 changed files with 59 additions and 9 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -24,7 +24,6 @@ package charts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
"github.com/wcharczuk/go-chart/v2/drawing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewMarkPoint returns a series mark point
|
// NewMarkPoint returns a series mark point
|
||||||
|
|
@ -78,16 +77,15 @@ func (m *markPointPainter) Render() (Box, error) {
|
||||||
symbolSize = 30
|
symbolSize = 30
|
||||||
}
|
}
|
||||||
textStyle := Style{
|
textStyle := Style{
|
||||||
FontColor: drawing.Color{
|
|
||||||
R: 238,
|
|
||||||
G: 238,
|
|
||||||
B: 238,
|
|
||||||
A: 255,
|
|
||||||
},
|
|
||||||
FontSize: labelFontSize,
|
FontSize: labelFontSize,
|
||||||
StrokeWidth: 1,
|
StrokeWidth: 1,
|
||||||
Font: opt.Font,
|
Font: opt.Font,
|
||||||
}
|
}
|
||||||
|
if isLightColor(opt.FillColor) {
|
||||||
|
textStyle.FontColor = defaultLightFontColor
|
||||||
|
} else {
|
||||||
|
textStyle.FontColor = defaultDarkFontColor
|
||||||
|
}
|
||||||
painter.OverrideDrawingStyle(Style{
|
painter.OverrideDrawingStyle(Style{
|
||||||
FillColor: opt.FillColor,
|
FillColor: opt.FillColor,
|
||||||
}).OverrideTextStyle(textStyle)
|
}).OverrideTextStyle(textStyle)
|
||||||
|
|
|
||||||
13
theme.go
13
theme.go
|
|
@ -76,6 +76,19 @@ const defaultFontSize = 12.0
|
||||||
|
|
||||||
var defaultTheme ColorPalette
|
var defaultTheme ColorPalette
|
||||||
|
|
||||||
|
var defaultLightFontColor = drawing.Color{
|
||||||
|
R: 70,
|
||||||
|
G: 70,
|
||||||
|
B: 70,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
|
var defaultDarkFontColor = drawing.Color{
|
||||||
|
R: 238,
|
||||||
|
G: 238,
|
||||||
|
B: 238,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
echartSeriesColors := []Color{
|
echartSeriesColors := []Color{
|
||||||
parseColor("#5470c6"),
|
parseColor("#5470c6"),
|
||||||
|
|
|
||||||
7
util.go
7
util.go
|
|
@ -262,3 +262,10 @@ func getPolygonPoints(center Point, radius float64, sides int) []Point {
|
||||||
}
|
}
|
||||||
return points
|
return points
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isLightColor(c Color) bool {
|
||||||
|
r := float64(c.R) * float64(c.R) * 0.299
|
||||||
|
g := float64(c.G) * float64(c.G) * 0.587
|
||||||
|
b := float64(c.B) * float64(c.B) * 0.114
|
||||||
|
return math.Sqrt(r+g+b) > 127.5
|
||||||
|
}
|
||||||
|
|
|
||||||
32
util_test.go
32
util_test.go
|
|
@ -189,3 +189,35 @@ func TestParseColor(t *testing.T) {
|
||||||
A: 250,
|
A: 250,
|
||||||
}, c)
|
}, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsLightColor(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
assert.True(isLightColor(drawing.Color{
|
||||||
|
R: 255,
|
||||||
|
G: 255,
|
||||||
|
B: 255,
|
||||||
|
}))
|
||||||
|
assert.True(isLightColor(drawing.Color{
|
||||||
|
R: 145,
|
||||||
|
G: 204,
|
||||||
|
B: 117,
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert.False(isLightColor(drawing.Color{
|
||||||
|
R: 88,
|
||||||
|
G: 112,
|
||||||
|
B: 198,
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert.False(isLightColor(drawing.Color{
|
||||||
|
R: 0,
|
||||||
|
G: 0,
|
||||||
|
B: 0,
|
||||||
|
}))
|
||||||
|
assert.False(isLightColor(drawing.Color{
|
||||||
|
R: 16,
|
||||||
|
G: 12,
|
||||||
|
B: 42,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue