diff options
author | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-23 18:12:03 -0500 |
---|---|---|
committer | Benjamin Chausse <benjamin@chausse.xyz> | 2024-11-23 18:12:03 -0500 |
commit | 89094fecf4cb1c018f15c976641cd18c255eac28 (patch) | |
tree | 9f6e32c38013bc526399ab324891e0b3269e50dc /internal/colors/precise.go |
Semi-working POC
Diffstat (limited to 'internal/colors/precise.go')
-rw-r--r-- | internal/colors/precise.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/internal/colors/precise.go b/internal/colors/precise.go new file mode 100644 index 0000000..fafa104 --- /dev/null +++ b/internal/colors/precise.go @@ -0,0 +1,38 @@ +package colors + +import ( + "fmt" + "math" + "strings" +) + +type ColorSpace interface { + ToPrecise() PreciseColor + FromPrecise(PreciseColor) ColorSpace +} + +// PreciseColor is a color with floating point values for red, green, and blue. +// The extra precision minimizes rounding errors when converting between different +// color spaces. It is used as an intermediate representation when converting between +// different color spaces. +type PreciseColor struct { + R, G, B float64 +} + +func (c PreciseColor) ToPrecise() PreciseColor { + return c +} + +func (c PreciseColor) FromPrecise(p PreciseColor) ColorSpace { + return p +} + +func ToHexString(cs ColorSpace) string { + p := cs.ToPrecise() + + return strings.ToUpper(fmt.Sprintf("#%02x%02x%02x", + int(math.Round(p.R*255)), + int(math.Round(p.G*255)), + int(math.Round(p.B*255)), + )) +} |