lucasb-eyer / go-colorful

A library for playing with colors in go (golang).
MIT License
1.16k stars 59 forks source link

Calling MakeColor on a color with a zero alpha value causes a panic #23

Closed ooesili closed 6 years ago

ooesili commented 6 years ago

If I call colorful.MakeColor on a color.Color with zero in the alpha channel, this happens:

panic: runtime error: integer divide by zero

goroutine 1 [running]:
github.com/lucasb-eyer/go-colorful.MakeColor(0x10c1c20, 0xc4200140f0, 0x10c1c20, 0xc4200140f0, 0xc42005bf78)
        /home/ooesili/go/src/github.com/lucasb-eyer/go-colorful/colors.go:31 +0xc8
main.main()
        /home/ooesili/go/src/github.com/ooesili/colorfultest/main.go:11 +0x57
exit status 2

The following code can be used to reproduce the issue:

package main

import (
    "image/color"

    colorful "github.com/lucasb-eyer/go-colorful"
)

func main() {
    c := color.RGBA{R: 0, B: 0, G: 0, A: 0}
    colorful.MakeColor(c)
}

I don't know really know what to expect in the return value (I'm not really much of color expert), but I'd assume it should return zero values for everything, since if every value in a color.Color is expected to be alpha-multiplied, and x * 0 == 0.

lucasb-eyer commented 6 years ago

Closing this as a duplicate of #21 and mentioned in the readme's FAQ.

I'm not sure setting everything to zero is the right call, since an undefined operation is asked (invert multiplication by zero; in your example, the code needs to figure out "what is x, and that is undefined). Though I agree panicing is not good either.

We could turn this into an error and let the user handle it, or give MakeColor an extra parameter with the value that should be used as the default in this case. Are you up to making a PR with one of those options?