Skip to content

Commit

Permalink
move the color types to the style package
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Jun 4, 2023
1 parent 38cc7c8 commit c5ee2ff
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 72 deletions.
31 changes: 0 additions & 31 deletions ui/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,3 @@ func degreesToRadians(d float64) float64 {
const halfcircle = 180 / math.Pi
return d / halfcircle
}

type color struct{ r, g, b float64 }

func (c color) toRGB() (r, g, b float64) {
return c.r, c.g, c.b
}

func (c color) toRGBA(alpha float64) (r, g, b, a float64) {
return c.r, c.g, c.b, alpha
}

type colorMap []color

func (c colorMap) toRGB(f float64) (r, g, b float64) {
f = math.Abs(f)
adaptedHeat := float64(f) * float64(len(c)-1)
colorIndex := int(adaptedHeat)
lower := c[int(math.Min(float64(colorIndex), float64(len(c)-1)))]
upper := c[int(math.Min(float64(colorIndex+1), float64(len(c)-1)))]
p := adaptedHeat - float64(colorIndex)
r = (1-p)*lower.r + p*upper.r
g = (1-p)*lower.g + p*upper.g
b = (1-p)*lower.b + p*upper.b
return
}

func (c colorMap) toRGBA(f float64, alpha float64) (r, g, b, a float64) {
r, g, b = c.toRGB(f)
a = alpha
return
}
8 changes: 5 additions & 3 deletions ui/lifetimeIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package ui
import (
"github.com/gotk3/gotk3/cairo"
"github.com/gotk3/gotk3/gtk"

"github.com/ftl/hellocontest/ui/style"
)

var lifetimeStyle = struct {
color color
color style.Color
}{
color: color{0, 0, 0},
color: style.Black,
}

type lifetimeIndicator struct {
Expand Down Expand Up @@ -36,7 +38,7 @@ func (ind *lifetimeIndicator) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
height := float64(da.GetAllocatedHeight())
width := lifetime * float64(da.GetAllocatedWidth())

cr.SetSourceRGB(lifetimeStyle.color.toRGB())
cr.SetSourceRGB(lifetimeStyle.color.ToRGB())
cr.MoveTo(0, 0)
cr.LineTo(width, 0)
cr.LineTo(width, height)
Expand Down
12 changes: 7 additions & 5 deletions ui/proximityIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (

"github.com/gotk3/gotk3/cairo"
"github.com/gotk3/gotk3/gtk"

"github.com/ftl/hellocontest/ui/style"
)

var proximityStyle = struct {
color color
colorExactMatch color
color style.Color
colorExactMatch style.Color
}{
color: color{0, 0, 0},
colorExactMatch: color{0, 255, 0},
color: style.Black,
colorExactMatch: style.Green,
}

type proximityIndicator struct {
Expand Down Expand Up @@ -51,7 +53,7 @@ func (ind *proximityIndicator) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
color = proximityStyle.colorExactMatch
}

cr.SetSourceRGB(color.toRGB())
cr.SetSourceRGB(color.ToRGB())
cr.MoveTo(0, startHeight)
cr.LineTo(width, startHeight)
cr.LineTo(width, endHeight)
Expand Down
56 changes: 29 additions & 27 deletions ui/rateIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,51 @@ import (
"math"
"time"

"github.com/ftl/hellocontest/core"
"github.com/gotk3/gotk3/cairo"
"github.com/gotk3/gotk3/gtk"

"github.com/ftl/hellocontest/core"
"github.com/ftl/hellocontest/ui/style"
)

var rateColors = colorMap{
var rateColors = style.ColorMap{
{1, 0, 0}, {1, 0.6, 0.2}, {0, 0.8, 0},
}

var timeColors = colorMap{
var timeColors = style.ColorMap{
{1, 0, 0}, {1, 0.6, 0.2}, {0, 0.8, 0}, {0, 0.8, 0}, {0, 0.8, 0}, {0, 0.8, 0},
}

const angleRotation = (3.0 / 2.0) * math.Pi

var rateStyle = struct {
backgroundColor color
fontColor color
backgroundColor style.Color
fontColor style.Color
fontSize float64
axisColor color
axisColor style.Color
axisMargin float64
lowZoneColor color
lowZoneColor style.Color
areaAlpha float64
borderAlpha float64
timeIndicatorWidth float64
timeFrameColor color
timeFrameColor style.Color
timeFrameAlpha float64
defaultScoreGraphColor color
scoreGraphColors map[core.Band]color
defaultScoreGraphColor style.Color
scoreGraphColors map[core.Band]style.Color
}{
backgroundColor: color{1, 1, 1},
fontColor: color{0.4, 0.4, 0.4},
backgroundColor: style.Color{1, 1, 1},
fontColor: style.Color{0.4, 0.4, 0.4},
fontSize: 15,
axisColor: color{0.4, 0.4, 0.4},
axisColor: style.Color{0.4, 0.4, 0.4},
axisMargin: 15,
lowZoneColor: color{0.8, 0.8, 0.8},
lowZoneColor: style.Color{0.8, 0.8, 0.8},
areaAlpha: 0.4,
borderAlpha: 0.8,
timeIndicatorWidth: 10,
timeFrameColor: color{1, 0.73, 0.2},
timeFrameColor: style.Color{1, 0.73, 0.2},
timeFrameAlpha: 1,
defaultScoreGraphColor: color{0.4, 0.4, 0.4},
scoreGraphColors: map[core.Band]color{
defaultScoreGraphColor: style.Color{0.4, 0.4, 0.4},
scoreGraphColors: map[core.Band]style.Color{
core.Band160m: {0.5, 0, 0.5},
core.Band80m: {0, 0, 0.5},
core.Band40m: {0, 1.0, 0},
Expand Down Expand Up @@ -97,29 +99,29 @@ func (ind *rateIndicator) Draw(da *gtk.DrawingArea, cr *cairo.Context) {

ind.fillBackground(cr)

cr.SetSourceRGBA(rateStyle.lowZoneColor.toRGBA(rateStyle.areaAlpha))
cr.SetSourceRGBA(rateStyle.lowZoneColor.ToRGBA(rateStyle.areaAlpha))
cr.MoveTo(ind.qAxis.goalPoint.x, ind.qAxis.goalPoint.y)
cr.LineTo(ind.pAxis.goalPoint.x, ind.pAxis.goalPoint.y)
cr.LineTo(ind.mAxis.goalPoint.x, ind.mAxis.goalPoint.y)
cr.ClosePath()
cr.Fill()

cr.SetSourceRGBA(rateStyle.lowZoneColor.toRGBA(rateStyle.borderAlpha))
cr.SetSourceRGBA(rateStyle.lowZoneColor.ToRGBA(rateStyle.borderAlpha))
cr.MoveTo(ind.qAxis.goalPoint.x, ind.qAxis.goalPoint.y)
cr.LineTo(ind.pAxis.goalPoint.x, ind.pAxis.goalPoint.y)
cr.LineTo(ind.mAxis.goalPoint.x, ind.mAxis.goalPoint.y)
cr.ClosePath()
cr.Stroke()

overallAchievment := (ind.qAxis.achievement + ind.pAxis.achievement + ind.mAxis.achievement) / 3
cr.SetSourceRGBA(rateColors.toRGBA(overallAchievment, rateStyle.areaAlpha))
cr.SetSourceRGBA(rateColors.ToRGBA(overallAchievment, rateStyle.areaAlpha))
cr.MoveTo(ind.qAxis.value1Point.x, ind.qAxis.value1Point.y)
cr.LineTo(ind.pAxis.value1Point.x, ind.pAxis.value1Point.y)
cr.LineTo(ind.mAxis.value1Point.x, ind.mAxis.value1Point.y)
cr.ClosePath()
cr.Fill()

cr.SetSourceRGBA(rateColors.toRGBA(overallAchievment, rateStyle.borderAlpha))
cr.SetSourceRGBA(rateColors.ToRGBA(overallAchievment, rateStyle.borderAlpha))
cr.MoveTo(ind.qAxis.value1Point.x, ind.qAxis.value1Point.y)
cr.LineTo(ind.pAxis.value1Point.x, ind.pAxis.value1Point.y)
cr.LineTo(ind.mAxis.value1Point.x, ind.mAxis.value1Point.y)
Expand All @@ -136,7 +138,7 @@ func (ind *rateIndicator) fillBackground(cr *cairo.Context) {
cr.Save()
defer cr.Restore()

cr.SetSourceRGB(rateStyle.backgroundColor.toRGB())
cr.SetSourceRGB(rateStyle.backgroundColor.ToRGB())
cr.Paint()
}

Expand Down Expand Up @@ -257,17 +259,17 @@ func (a *rateAxis) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
cr.Save()
defer cr.Restore()

cr.SetSourceRGB(rateStyle.axisColor.toRGB())
cr.SetSourceRGB(rateStyle.axisColor.ToRGB())
cr.MoveTo(a.axisLine.left, a.axisLine.top)
cr.LineTo(a.axisLine.right, a.axisLine.bottom)
cr.Stroke()

cr.SetSourceRGB(rateStyle.fontColor.toRGB())
cr.SetSourceRGB(rateStyle.fontColor.ToRGB())
cr.SetFontSize(rateStyle.fontSize)
cr.MoveTo(a.labelRect.left, a.labelRect.bottom)
cr.ShowText(a.LabelText())

cr.SetSourceRGB(rateColors.toRGB(a.achievement))
cr.SetSourceRGB(rateColors.ToRGB(a.achievement))
cr.Arc(a.value1Point.x, a.value1Point.y, 5, 0, 2*math.Pi)
cr.Fill()
cr.Stroke()
Expand Down Expand Up @@ -337,12 +339,12 @@ func (ind *timeIndicator) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
radius := (math.Min(float64(da.GetAllocatedWidth()), float64(da.GetAllocatedHeight())) / 2) - (ind.lineWidth / 2)
angle := (1 - ind.achievement) * 2 * math.Pi

cr.SetSourceRGB(timeColors.toRGB(ind.achievement))
cr.SetSourceRGB(timeColors.ToRGB(ind.achievement))
cr.SetLineWidth(ind.lineWidth)
cr.Arc(center.x, center.y, radius, angleRotation, angle+angleRotation)
cr.Stroke()

cr.SetSourceRGB(rateStyle.fontColor.toRGB())
cr.SetSourceRGB(rateStyle.fontColor.ToRGB())
cr.SetFontSize(rateStyle.fontSize)
labelExtents := cr.TextExtents(ind.labelText)
label := point{
Expand Down
12 changes: 6 additions & 6 deletions ui/scoreGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func (g *scoreGraph) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
g.fillBackground(cr)

// the zone
cr.SetSourceRGBA(rateStyle.lowZoneColor.toRGBA(rateStyle.areaAlpha))
cr.SetSourceRGBA(rateStyle.lowZoneColor.ToRGBA(rateStyle.areaAlpha))
cr.MoveTo(0, layout.zeroY-layout.lowZoneHeight)
cr.LineTo(layout.width, layout.zeroY-layout.lowZoneHeight)
cr.LineTo(layout.width, layout.zeroY+layout.lowZoneHeight)
cr.LineTo(0, layout.zeroY+layout.lowZoneHeight)
cr.ClosePath()
cr.Fill()

cr.SetSourceRGBA(rateStyle.lowZoneColor.toRGBA(rateStyle.borderAlpha))
cr.SetSourceRGBA(rateStyle.lowZoneColor.ToRGBA(rateStyle.borderAlpha))
cr.MoveTo(0, layout.zeroY-layout.lowZoneHeight)
cr.LineTo(layout.width, layout.zeroY-layout.lowZoneHeight)
cr.LineTo(layout.width, layout.zeroY+layout.lowZoneHeight)
Expand All @@ -108,7 +108,7 @@ func (g *scoreGraph) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
if !ok {
color = rateStyle.defaultScoreGraphColor
}
cr.SetSourceRGB(color.toRGB())
cr.SetSourceRGB(color.ToRGB())

g.drawDataPoints(cr, layout, graph.DataPoints)
}
Expand All @@ -117,7 +117,7 @@ func (g *scoreGraph) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
if g.timeFrameIndex >= 0 && valueCount > 1 {
startX := float64(g.timeFrameIndex) * layout.binWidth
endX := float64(g.timeFrameIndex+1) * layout.binWidth
cr.SetSourceRGBA(rateStyle.timeFrameColor.toRGBA(rateStyle.timeFrameAlpha)) // TODO calculate the achievment of the current time frame and use the corresponding color
cr.SetSourceRGBA(rateStyle.timeFrameColor.ToRGBA(rateStyle.timeFrameAlpha)) // TODO calculate the achievment of the current time frame and use the corresponding color
cr.MoveTo(startX, layout.zeroY-layout.maxHeight)
cr.LineTo(endX, layout.zeroY-layout.maxHeight)
cr.LineTo(endX, layout.zeroY+layout.maxHeight)
Expand All @@ -127,7 +127,7 @@ func (g *scoreGraph) Draw(da *gtk.DrawingArea, cr *cairo.Context) {
}

// the zero line
cr.SetSourceRGB(rateStyle.axisColor.toRGB())
cr.SetSourceRGB(rateStyle.axisColor.ToRGB())
cr.MoveTo(0, layout.zeroY)
cr.LineTo(layout.width, layout.zeroY)
cr.Stroke()
Expand All @@ -152,7 +152,7 @@ func (g *scoreGraph) fillBackground(cr *cairo.Context) {
cr.Save()
defer cr.Restore()

cr.SetSourceRGB(rateStyle.backgroundColor.toRGB())
cr.SetSourceRGB(rateStyle.backgroundColor.ToRGB())
cr.Paint()
}

Expand Down
44 changes: 44 additions & 0 deletions ui/style/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package style
import (
_ "embed"
"log"
"math"

"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
Expand All @@ -13,6 +14,45 @@ type Class string
//go:embed contest.css
var css string

type Color struct{ R, G, B float64 }

var (
Black = Color{}
White = Color{1, 1, 1}
Red = Color{1, 0, 0}
Green = Color{0, 1, 0}
Blue = Color{0, 0, 1}
)

func (c Color) ToRGB() (r, g, b float64) {
return c.R, c.G, c.B
}

func (c Color) ToRGBA(alpha float64) (r, g, b, a float64) {
return c.R, c.G, c.B, alpha
}

type ColorMap []Color

func (c ColorMap) ToRGB(f float64) (r, g, b float64) {
f = math.Abs(f)
adaptedHeat := float64(f) * float64(len(c)-1)
colorIndex := int(adaptedHeat)
lower := c[int(math.Min(float64(colorIndex), float64(len(c)-1)))]
upper := c[int(math.Min(float64(colorIndex+1), float64(len(c)-1)))]
p := adaptedHeat - float64(colorIndex)
r = (1-p)*lower.R + p*upper.R
g = (1-p)*lower.G + p*upper.G
b = (1-p)*lower.B + p*upper.B
return
}

func (c ColorMap) ToRGBA(f float64, alpha float64) (r, g, b, a float64) {
r, g, b = c.ToRGB(f)
a = alpha
return
}

type Style struct {
provider *gtk.CssProvider
}
Expand Down Expand Up @@ -42,6 +82,10 @@ func (s *Style) AddToScreen(screen *gdk.Screen) {
gtk.AddProviderForScreen(screen, s.provider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
}

func (s *Style) FindColor(name string) Color {
return Black
}

func AddClass(widget *gtk.Widget, class Class) {
err := doWithStyle(widget, func(context *gtk.StyleContext) {
context.AddClass(string(class))
Expand Down

0 comments on commit c5ee2ff

Please sign in to comment.