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>
This commit is contained in:
Egon Elbre
2020-11-18 20:21:26 +02:00
committed by Elias Naur
parent 9469d18907
commit 21ef492cc9
23 changed files with 200 additions and 157 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ import (
// Border lays out a widget and draws a border inside it.
type Border struct {
Color color.RGBA
Color color.NRGBA
CornerRadius unit.Value
Width unit.Value
}
+5 -4
View File
@@ -7,6 +7,7 @@ import (
"image/color"
"image/draw"
"gioui.org/internal/f32color"
"gioui.org/layout"
"gioui.org/op/paint"
"gioui.org/unit"
@@ -14,12 +15,12 @@ import (
)
type Icon struct {
Color color.RGBA
Color color.NRGBA
src []byte
// Cached values.
op paint.ImageOp
imgSize int
imgColor color.RGBA
imgColor color.NRGBA
}
// NewIcon returns a new Icon from IconVG data.
@@ -28,7 +29,7 @@ func NewIcon(data []byte) (*Icon, error) {
if err != nil {
return nil, err
}
return &Icon{src: data, Color: color.RGBA{A: 0xff}}, nil
return &Icon{src: data, Color: color.NRGBA{A: 0xff}}, nil
}
func (ic *Icon) Layout(gtx layout.Context, sz unit.Value) layout.Dimensions {
@@ -49,7 +50,7 @@ func (ic *Icon) image(sz int) paint.ImageOp {
img := image.NewRGBA(image.Rectangle{Max: image.Point{X: sz, Y: int(float32(sz) * dy / dx)}})
var ico iconvg.Rasterizer
ico.SetDstImage(img, img.Bounds(), draw.Src)
m.Palette[0] = ic.Color
m.Palette[0] = f32color.NRGBAToRGBA(ic.Color)
iconvg.Decode(&ico, ic.src, &iconvg.DecodeOptions{
Palette: &m.Palette,
})
+6 -6
View File
@@ -22,10 +22,10 @@ import (
type ButtonStyle struct {
Text string
// Color is the text color.
Color color.RGBA
Color color.NRGBA
Font text.Font
TextSize unit.Value
Background color.RGBA
Background color.NRGBA
CornerRadius unit.Value
Inset layout.Inset
Button *widget.Clickable
@@ -33,15 +33,15 @@ type ButtonStyle struct {
}
type ButtonLayoutStyle struct {
Background color.RGBA
Background color.NRGBA
CornerRadius unit.Value
Button *widget.Clickable
}
type IconButtonStyle struct {
Background color.RGBA
Background color.NRGBA
// Color is the icon color.
Color color.RGBA
Color color.NRGBA
Icon *widget.Icon
// Size is the icon size.
Size unit.Value
@@ -272,7 +272,7 @@ func drawInk(gtx layout.Context, c widget.Press) {
const col = 0.8
ba, bc := byte(alpha*0xff), byte(col*0xff)
defer op.Push(gtx.Ops).Pop()
rgba := f32color.MulAlpha(color.RGBA{A: 0xff, R: bc, G: bc, B: bc}, ba)
rgba := f32color.MulAlpha(color.NRGBA{A: 0xff, R: bc, G: bc, B: bc}, ba)
ink := paint.ColorOp{Color: rgba}
ink.Add(gtx.Ops)
rr := size * .5
+2 -2
View File
@@ -17,10 +17,10 @@ import (
type checkable struct {
Label string
Color color.RGBA
Color color.NRGBA
Font text.Font
TextSize unit.Value
IconColor color.RGBA
IconColor color.NRGBA
Size unit.Value
shaper text.Shaper
checkedStateIcon *widget.Icon
+1 -1
View File
@@ -36,7 +36,7 @@
// Theme-global parameters: For changing the look of all widgets drawn with a
// particular theme, adjust the `Theme` fields:
//
// theme.Color.Primary = color.RGBA{...}
// theme.Color.Primary = color.NRGBA{...}
//
// Widget-local parameters: For changing the look of a particular widget,
// adjust the widget specific theme object:
+2 -2
View File
@@ -18,11 +18,11 @@ type EditorStyle struct {
Font text.Font
TextSize unit.Value
// Color is the text color.
Color color.RGBA
Color color.NRGBA
// Hint contains the text displayed when the editor is empty.
Hint string
// HintColor is the color of hint text.
HintColor color.RGBA
HintColor color.NRGBA
Editor *widget.Editor
shaper text.Shaper
+1 -1
View File
@@ -16,7 +16,7 @@ type LabelStyle struct {
// Face defines the text style.
Font text.Font
// Color is the text color.
Color color.RGBA
Color color.NRGBA
// Alignment specify the text alignment.
Alignment text.Alignment
// MaxLines limits the number of lines. Zero means no limit.
+1 -1
View File
@@ -17,7 +17,7 @@ import (
)
type LoaderStyle struct {
Color color.RGBA
Color color.NRGBA
}
func Loader(th *Theme) LoaderStyle {
+2 -2
View File
@@ -15,7 +15,7 @@ import (
)
type ProgressBarStyle struct {
Color color.RGBA
Color color.NRGBA
Progress int
}
@@ -27,7 +27,7 @@ func ProgressBar(th *Theme, progress int) ProgressBarStyle {
}
func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
shader := func(width float32, color color.RGBA) layout.Dimensions {
shader := func(width float32, color color.NRGBA) layout.Dimensions {
maxHeight := unit.Dp(4)
rr := float32(gtx.Px(unit.Dp(2)))
+1 -1
View File
@@ -28,7 +28,7 @@ func Slider(th *Theme, float *widget.Float, min, max float32) SliderStyle {
type SliderStyle struct {
Min, Max float32
Color color.RGBA
Color color.NRGBA
Float *widget.Float
}
+3 -3
View File
@@ -19,8 +19,8 @@ import (
type SwitchStyle struct {
Color struct {
Enabled color.RGBA
Disabled color.RGBA
Enabled color.NRGBA
Disabled color.NRGBA
}
Switch *widget.Bool
}
@@ -124,7 +124,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: dims}
}
func drawDisc(ops *op.Ops, sz float32, col color.RGBA) {
func drawDisc(ops *op.Ops, sz float32, col color.NRGBA) {
defer op.Push(ops).Pop()
rr := sz / 2
r := f32.Rectangle{Max: f32.Point{X: sz, Y: sz}}
+7 -7
View File
@@ -14,10 +14,10 @@ import (
type Theme struct {
Shaper text.Shaper
Color struct {
Primary color.RGBA
Text color.RGBA
Hint color.RGBA
InvText color.RGBA
Primary color.NRGBA
Text color.NRGBA
Hint color.NRGBA
InvText color.NRGBA
}
TextSize unit.Value
Icon struct {
@@ -53,10 +53,10 @@ func mustIcon(ic *widget.Icon, err error) *widget.Icon {
return ic
}
func rgb(c uint32) color.RGBA {
func rgb(c uint32) color.NRGBA {
return argb(0xff000000 | c)
}
func argb(c uint32) color.RGBA {
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(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)}
}