File: hsva.go

package info (click to toggle)
golang-gonum-v1-plot 0.7.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,980 kB
  • sloc: sh: 81; makefile: 13
file content (116 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Copyright ©2011-2013 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package palette

import (
	"image/color"
	"math"
)

// HSVA represents a Hue/Saturation/Value/Alpha color.
// H, S, V and A are valid within [0, 1].
type HSVA struct {
	H, S, V, A float64
}

// HSVAModel converts any color.Color to an HSVA color.
var HSVAModel = color.ModelFunc(hsvaModel)

func hsvaModel(c color.Color) color.Color {
	if _, ok := c.(HSVA); ok {
		return c
	}
	return rgbaToHsva(c.RGBA())
}

// Convert r, g, b, a to HSVA
func rgbaToHsva(r, g, b, a uint32) HSVA {
	red := float64(r)
	blue := float64(b)
	green := float64(g)

	max := math.Max(red, green)
	max = math.Max(max, blue)
	min := math.Min(red, green)
	min = math.Min(min, blue)
	chroma := max - min

	var hue float64
	switch {
	case chroma == 0:
		// This should really be math.NaN() since we have a 0 length vector,
		// but 0 seems to be the convention and it may simplify imports in
		// dependent packages.
		hue = 0
	case max == red:
		hue = math.Mod((green-blue)/chroma, 6)
	case max == green:
		hue = (blue-red)/chroma + 2
	case max == blue:
		hue = (red-green)/chroma + 4
	}

	hue /= 6

	var s float64
	if chroma != 0 {
		s = chroma / max
	}

	return HSVA{
		H: math.Mod(math.Mod(hue, 1)+1, 1),
		S: s,
		V: max / math.MaxUint16,
		A: float64(a) / math.MaxUint16,
	}
}

// RGBA allows HSVAColor to satisfy the color.Color interface.
func (c HSVA) RGBA() (r, g, b, a uint32) {
	var red, green, blue float64

	a = uint32(math.MaxUint16 * c.A)

	if c.V == 0 {
		return
	}

	if c.S == 0 {
		r, g, b = uint32(math.MaxUint16*c.V), uint32(math.MaxUint16*c.V), uint32(math.MaxUint16*c.V)
		return
	}

	chroma := c.V * c.S
	m := c.V - chroma

	if !math.IsNaN(c.H) {
		hue := math.Mod(c.H, 1) * 6
		x := chroma * (1 - math.Abs(math.Mod(hue, 2)-1))
		switch math.Floor(hue) {
		case 0:
			red, green = chroma, x
		case 1:
			red, green = x, chroma
		case 2:
			green, blue = chroma, x
		case 3:
			green, blue = x, chroma
		case 4:
			red, blue = x, chroma
		case 5:
			red, blue = chroma, x
		}
	} else {
		red, green, blue = 0, 0, 0
	}

	r, g, b = uint32(math.MaxUint16*(red+m)), uint32(math.MaxUint16*(green+m)), uint32(math.MaxUint16*(blue+m))

	return
}