color.go

Functions Structs

Functions

func Color

Color converts RGBA to the standard color.Color interface.

 

Deprecated: gg.RGBA now implements color.Color directly.

Use the value itself instead of calling .Color().

func (c RGBA) Color() color.Color {
	return color.NRGBA{
		R:	uint8(clamp255(c.R * 255)),
		G:	uint8(clamp255(c.G * 255)),
		B:	uint8(clamp255(c.B * 255)),
		A:	uint8(clamp255(c.A * 255)),
	}
}

func FromColor

FromColor converts a standard color.Color to straight-alpha RGBA.

Go's color.Color.RGBA() returns premultiplied values; this un-premultiplies them.

func FromColor(c color.Color) RGBA {
	r, g, b, a := c.RGBA()
	if a == 0 {
		return RGBA{0, 0, 0, 0}
	}
	return RGBA{
		R:	float64(r) / float64(a),
		G:	float64(g) / float64(a),
		B:	float64(b) / float64(a),
		A:	float64(a) / 65535,
	}
}

func HSL

HSL creates a color from HSL values.

h is hue [0, 360), s is saturation [0, 1], l is lightness [0, 1].

func HSL(h, s, l float64) RGBA {
	h = math.Mod(h, 360)
	if h < 0 {
		h += 360
	}
	h /= 360

	c := (1 - math.Abs(2*l-1)) * s
	x := c * (1 - math.Abs(math.Mod(h*6, 2)-1))
	m := l - c/2

	var r, g, b float64
	switch {
	case h < 1.0/6:
		r, g, b = c, x, 0
	case h < 2.0/6:
		r, g, b = x, c, 0
	case h < 3.0/6:
		r, g, b = 0, c, x
	case h < 4.0/6:
		r, g, b = 0, x, c
	case h < 5.0/6:
		r, g, b = x, 0, c
	default:
		r, g, b = c, 0, x
	}

	return RGB(r+m, g+m, b+m)
}

func Hex

Hex creates a color from a hex string.

Supports formats: "RGB", "RGBA", "RRGGBB", "RRGGBBAA".

func Hex(hex string) RGBA {
	if c, err := ParseHex(hex); err == nil {
		return c
	}
	return RGBA{R: 0, G: 0, B: 0, A: 1}
}

func Lerp

Lerp performs linear interpolation between two colors.

func (c RGBA) Lerp(other RGBA, t float64) RGBA {
	return RGBA{
		R:	c.R + (other.R-c.R)*t,
		G:	c.G + (other.G-c.G)*t,
		B:	c.B + (other.B-c.B)*t,
		A:	c.A + (other.A-c.A)*t,
	}
}

func ParseHex

ParseHex creates a color from a hex string, returning an error for invalid formats.

Supports formats: "RGB", "RGBA", "RRGGBB", "RRGGBBAA".

func ParseHex(hex string) (RGBA, error) {
	original := hex
	if hex != "" && hex[0] == '#' {
		hex = hex[1:]
	}

	var r, g, b, a uint32
	a = 255
	var valid bool

	switch len(hex) {
	case 3:	// RGB
		valid = parseHex(hex[0:1], &r) && parseHex(hex[1:2], &g) && parseHex(hex[2:3], &b)
		r, g, b = r*17, g*17, b*17
	case 4:	// RGBA
		valid = parseHex(hex[0:1], &r) && parseHex(hex[1:2], &g) && parseHex(hex[2:3], &b) && parseHex(hex[3:4], &a)
		r, g, b, a = r*17, g*17, b*17, a*17
	case 6:	// RRGGBB
		valid = parseHex(hex[0:2], &r) && parseHex(hex[2:4], &g) && parseHex(hex[4:6], &b)
	case 8:	// RRGGBBAA
		valid = parseHex(hex[0:2], &r) && parseHex(hex[2:4], &g) && parseHex(hex[4:6], &b) && parseHex(hex[6:8], &a)
	default:
		valid = false
	}

	if !valid {
		return RGBA{R: 0, G: 0, B: 0, A: 1}, fmt.Errorf("invalid hex color format: %q", original)
	}

	return RGBA{
		R:	float64(r) / 255,
		G:	float64(g) / 255,
		B:	float64(b) / 255,
		A:	float64(a) / 255,
	}, nil
}

func Premultiply

Premultiply returns a premultiplied color.

func (c RGBA) Premultiply() RGBA {
	return RGBA{
		R:	c.R * c.A,
		G:	c.G * c.A,
		B:	c.B * c.A,
		A:	c.A,
	}
}

func RGB

RGB creates an opaque color from RGB components.

func RGB(r, g, b float64) RGBA {
	return RGBA{R: r, G: g, B: b, A: 1.0}
}

func RGBA

RGBA implements the color.Color interface.

Returns premultiplied alpha values scaled to [0, 65535] as required by the interface.

This allows gg.RGBA to be used directly with dc.SetColor(gg.Black).

func (c RGBA) RGBA() (r, g, b, a uint32) {
	a = uint32(clamp65535(c.A * 65535))
	r = uint32(clamp65535(c.R * c.A * 65535))
	g = uint32(clamp65535(c.G * c.A * 65535))
	b = uint32(clamp65535(c.B * c.A * 65535))
	return
}

func RGBA2

RGBA2 creates a color from RGBA components.

func RGBA2(r, g, b, a float64) RGBA {
	return RGBA{R: r, G: g, B: b, A: a}
}

func Unpremultiply

Unpremultiply returns an unpremultiplied color.

func (c RGBA) Unpremultiply() RGBA {
	if c.A == 0 {
		return RGBA{R: 0, G: 0, B: 0, A: 0}
	}
	return RGBA{
		R:	c.R / c.A,
		G:	c.G / c.A,
		B:	c.B / c.A,
		A:	c.A,
	}
}

Structs

type RGBA struct

RGBA represents a color with red, green, blue, and alpha components.

Each component is in the range [0, 1].

type RGBA struct {
	R, G, B, A float64
}