fyne-io / fyne

Cross platform GUI toolkit in Go inspired by Material Design
https://fyne.io/
Other
25.25k stars 1.4k forks source link

Canvas text with alpha transparency doesn't render correctly #5275

Closed ncw closed 1 week ago

ncw commented 1 week ago

Checklist

Describe the bug

Canvas text with alpha transparency doesn't render correctly.

If you run the program below it should produce two texts of very nearly the same color, but it produces one black and one red.

The difference is that the red text has alpha 255 whereas the black text has alpha 254

How to reproduce

Run the example code

Screenshots

image

Example code

package main

import (
    "image/color"

    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
)

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Red Text Canvas")

    text1 := canvas.NewText("This is some red text", color.RGBA{R: 255, G: 0, B: 0, A: 255})
    text1.TextSize = 30
    text1.TextStyle = fyne.TextStyle{Bold: true}
    text1.Move(fyne.NewPos(0, 0))

    text2 := canvas.NewText("Slighty transparent red", color.RGBA{R: 255, G: 0, B: 0, A: 254})
    text2.TextSize = 30
    text2.TextStyle = fyne.TextStyle{Bold: true}
    text2.Move(fyne.NewPos(0, 50))

    content := container.NewWithoutLayout(text1, text2)
    myWindow.SetContent(content)

    myWindow.Resize(fyne.NewSize(400, 300))
    myWindow.ShowAndRun()
}

Fyne version

v2.5.3-0.20241117144446-a48acbbf88de (latest develop)

Go compiler version

go1.23.2

Operating system and version

Ubuntu 22.04.5 LTS

Additional Information

No response

andydotxyz commented 1 week ago

This is a confusion with how color.RGBA works. Switch it to color.NRGBA and it will work as planned.

The reason is that RGBA is "pre-multiplied" so any value of R, G or B that is greater than A is invalid. NRGBA is what you would expect from hex codes or CSS values.

ncw commented 1 week ago

Thanks for the explanation and sorry for the noise.