Debian Codegraph @

resources/images/color.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package images
15
16 import (
17 "encoding/hex"
18 "fmt"
19 "image/color"
20 "math"
21 "slices"
22 "strings"
23
24 "github.com/gohugoio/hugo/common/hstrings"
25 )
26
27 type colorGoProvider interface {
28 ColorGo() color.Color
29 }
30
31 type Color struct {
32 // The color.
33 color color.Color
34
35 // The color prefixed with a #.
36 hex string
37
38 // The relative luminance of the color.
39 luminance float64
40 }
41
42 // Luminance as defined by w3.org.
43 // See https://www.w3.org/TR/WCAG21/#dfn-relative-luminance
44 func (c Color) Luminance() float64 {
45 return c.luminance
46 }
47
48 // ColorGo returns the color as a color.Color.
49 // For internal use only.
50 func (c Color) ColorGo() color.Color {
51 return c.color
52 }
53
54 // ColorHex returns the color as a hex string prefixed with a #.
55 func (c Color) ColorHex() string {
56 return c.hex
57 }
58
59 // String returns the color as a hex string prefixed with a #.
60 func (c Color) String() string {
61 return c.hex
62 }
63
64 // For hashstructure. This struct is used in template func options
65 // that needs to be able to hash a Color.
66 // For internal use only.
67 func (c Color) Key() string {
68 return c.hex
69 }
70
71 func (c *Color) init() error {
72 c.hex = ColorGoToHexString(c.color)
73 r, g, b, _ := c.color.RGBA()
74 c.luminance = 0.2126*c.toSRGB(uint8(r)) + 0.7152*c.toSRGB(uint8(g)) + 0.0722*c.toSRGB(uint8(b))
75 return nil
76 }
77
78 func (c Color) toSRGB(i uint8) float64 {
79 v := float64(i) / 255
80 if v <= 0.04045 {
81 return v / 12.92
82 } else {
83 return math.Pow((v+0.055)/1.055, 2.4)
84 }
85 }
86
87 // AddColorToPalette adds c as the first color in p if not already there.
88 // Note that it does no additional checks, so callers must make sure
89 // that the palette is valid for the relevant format.
90 func AddColorToPalette(c color.Color, p color.Palette) color.Palette {
91 var found bool
92 if slices.Contains(p, c) {
93 found = true
94 }
95
96 if !found {
97 p = append(color.Palette{c}, p...)
98 }
99
100 return p
101 }
102
103 // ReplaceColorInPalette will replace the color in palette p closest to c in Euclidean
104 // R,G,B,A space with c.
105 func ReplaceColorInPalette(c color.Color, p color.Palette) {
106 p[p.Index(c)] = c
107 }
108
109 // ColorGoToHexString converts a color.Color to a hex string.
110 func ColorGoToHexString(c color.Color) string {
111 r, g, b, a := c.RGBA()
112 rgba := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
113 if rgba.A == 0xff {
114 return fmt.Sprintf("#%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
115 }
116 return fmt.Sprintf("#%.2x%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B, rgba.A)
117 }
118
119 // ColorGoToColor converts a color.Color to a Color.
120 func ColorGoToColor(c color.Color) Color {
121 cc := Color{color: c}
122 if err := cc.init(); err != nil {
123 panic(err)
124 }
125 return cc
126 }
127
128 func hexStringToColor(s string) Color {
129 c, err := hexStringToColorGo(s)
130 if err != nil {
131 panic(err)
132 }
133 return ColorGoToColor(c)
134 }
135
136 // HexStringsToColors converts a slice of hex strings to a slice of Colors.
137 func HexStringsToColors(s ...string) []Color {
138 var colors []Color
139 for _, v := range s {
140 colors = append(colors, hexStringToColor(v))
141 }
142 return colors
143 }
144
145 func toColorGo(v any) (color.Color, bool, error) {
146 switch vv := v.(type) {
147 case colorGoProvider:
148 return vv.ColorGo(), true, nil
149 default:
150 s, ok := hstrings.ToString(v)
151 if !ok {
152 return nil, false, nil
153 }
154 c, err := hexStringToColorGo(s)
155 if err != nil {
156 return nil, false, err
157 }
158 return c, true, nil
159 }
160 }
161
162 func hexStringToColorGo(s string) (color.Color, error) {
163 s = strings.TrimPrefix(s, "#")
164
165 if len(s) != 3 && len(s) != 4 && len(s) != 6 && len(s) != 8 {
166 return nil, fmt.Errorf("invalid color code: %q", s)
167 }
168
169 s = strings.ToLower(s)
170
171 if len(s) == 3 || len(s) == 4 {
172 var v strings.Builder
173 for _, r := range s {
174 v.WriteString(string(r) + string(r))
175 }
176 s = v.String()
177 }
178
179 // Standard colors.
180 if s == "ffffff" {
181 return color.White, nil
182 }
183
184 if s == "000000" {
185 return color.Black, nil
186 }
187
188 // Set Alfa to white.
189 if len(s) == 6 {
190 s += "ff"
191 }
192
193 b, err := hex.DecodeString(s)
194 if err != nil {
195 return nil, err
196 }
197
198 return color.RGBA{b[0], b[1], b[2], b[3]}, nil
199 }