~eliasnaur/colorspace

ref: 0de846b04b43 colorspace/srgb.go -rw-r--r-- 2.1 KiB
0de846b0Elias Naur implement image.RGBA64Image, draw.RGBA64Image support 5 months ago
                                                                                
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
// SPDX-License-Identifier: Unlicense

// Package colorspace implements color space conversion wrappers
// compatible with image.Image.
//
// It is a proof of concept for https://github.com/golang/go/issues/11420.
package colorspace

import (
	"image"
	"image/color"
	"math"

	"golang.org/x/image/draw"
)

// SRGBA wraps an Image encoded in the sRGB color space.
type SRGBA struct {
	image.RGBA64Image
}

// NewSRGBA returns an SRGBA that wraps img. It panics if ima
// doesn't implement image.RGBA64Image.
func NewSRGBA(img image.Image) *SRGBA {
	return &SRGBA{img.(image.RGBA64Image)}
}

// RGBA64At converts an sRGB color from the underlying image to linear RGB.
func (p *SRGBA) RGBA64At(x, y int) color.RGBA64 {
	c := p.RGBA64Image.RGBA64At(x, y)
	ret := color.RGBA64{
		uint16(sRGBToLinear(float32(c.R)/0xffff)*0xffff + .5),
		uint16(sRGBToLinear(float32(c.G)/0xffff)*0xffff + .5),
		uint16(sRGBToLinear(float32(c.B)/0xffff)*0xffff + .5),
		c.A,
	}
	return ret
}

// At converts an sRGB color from the underlying image to linear RGB.
func (p *SRGBA) At(x, y int) color.Color {
	return p.RGBA64At(x, y)
}

// Set converts a linear RGB color to sRGB and stores it in the underlying image.
func (p *SRGBA) Set(x, y int, c color.Color) {
	r, g, b, a := c.RGBA()
	p.SetRGBA64(x, y, color.RGBA64{uint16(r), uint16(g), uint16(b), uint16(a)})
}

// SetRGBA64 converts a linear RGB color to sRGB and stores it in the underlying image.
func (p *SRGBA) SetRGBA64(x, y int, c color.RGBA64) {
	srgb := color.RGBA64{
		uint16(linearTosRGB(float32(c.R)/0xffff)*0xffff + .5),
		uint16(linearTosRGB(float32(c.G)/0xffff)*0xffff + .5),
		uint16(linearTosRGB(float32(c.B)/0xffff)*0xffff + .5),
		c.A,
	}
	p.RGBA64Image.(draw.RGBA64Image).SetRGBA64(x, y, srgb)
}

func linearTosRGB(c float32) float32 {
	// Formula from EXT_sRGB.
	switch {
	case c <= 0:
		return 0
	case 0 < c && c < 0.0031308:
		return 12.92 * c
	case 0.0031308 <= c && c < 1:
		return 1.055*float32(math.Pow(float64(c), 0.41666)) - 0.055
	}
	return 1
}

func sRGBToLinear(c float32) float32 {
	// Formula from EXT_sRGB.
	if c <= 0.04045 {
		return c / 12.92
	} else {
		return float32(math.Pow(float64((c+0.055)/1.055), 2.4))
	}
}