Files
gio-patched/widget/material/theme.go
T
Egon Elbre 21ef492cc9 all: use color.NRGBA in public API
color.RGBA has two problems with regards to using it.

First the color values need to be premultiplied, whereas most APIs
have non-premultiplied values. This is mainly to preserve color components
with low alpha values.

Second there are two ways to premultiply with sRGB. One is to premultiply
after sRGB conversion, the other is before. This makes using the API more
confusing.

Using color.NRGBA in sRGB makes it align with CSS.e

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-19 11:30:11 +01:00

63 lines
1.4 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image/color"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"golang.org/x/exp/shiny/materialdesign/icons"
)
type Theme struct {
Shaper text.Shaper
Color struct {
Primary color.NRGBA
Text color.NRGBA
Hint color.NRGBA
InvText color.NRGBA
}
TextSize unit.Value
Icon struct {
CheckBoxChecked *widget.Icon
CheckBoxUnchecked *widget.Icon
RadioChecked *widget.Icon
RadioUnchecked *widget.Icon
}
}
func NewTheme(fontCollection []text.FontFace) *Theme {
t := &Theme{
Shaper: text.NewCache(fontCollection),
}
t.Color.Primary = rgb(0x3f51b5)
t.Color.Text = rgb(0x000000)
t.Color.Hint = rgb(0xbbbbbb)
t.Color.InvText = rgb(0xffffff)
t.TextSize = unit.Sp(16)
t.Icon.CheckBoxChecked = mustIcon(widget.NewIcon(icons.ToggleCheckBox))
t.Icon.CheckBoxUnchecked = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank))
t.Icon.RadioChecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked))
t.Icon.RadioUnchecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked))
return t
}
func mustIcon(ic *widget.Icon, err error) *widget.Icon {
if err != nil {
panic(err)
}
return ic
}
func rgb(c uint32) color.NRGBA {
return argb(0xff000000 | c)
}
func argb(c uint32) color.NRGBA {
return color.NRGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
}