Debian Codegraph @

docs/content/en/methods/resource/Colors.md

raw
1 ---
2 title: Colors
3 description: Applicable to images, returns a slice of the most dominant colors using a simple histogram method.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: '[]images.Color'
9 signatures: [RESOURCE.Colors]
10 ---
11
12 {{% include "/_common/methods/resource/global-page-remote-resources.md" %}}
13
14 The `Colors` method returns a slice of the most dominant colors in a [processable image](g), ordered from most dominant to least dominant.
15
16 > [!NOTE]
17 > Use the [`reflect.IsImageResourceProcessable`][] function to verify that an image can be processed.
18
19 ## Usage
20
21 This method is fast, but if you downscale your image first, you can further improve performance by extracting colors from the smaller resource.
22
23 ## Methods
24
25 Each color in the slice is an object with the following methods:
26
27 `ColorHex`
28 : (`string`) Returns the [hexadecimal color][] value, prefixed with a hash sign.
29
30 `Luminance`
31 : (`float64`) Returns the [relative luminance][] of the color in the sRGB colorspace in the range `[0, 1]`. A value of `0` represents the darkest black, while a value of `1` represents the lightest white.
32
33 > [!NOTE]
34 > Image filters such as [`images.Dither`][], [`images.Padding`][], and [`images.Text`][] accept either hexadecimal color values or `images.Color` objects as arguments. Hugo renders an `images.Color` object as a hexadecimal color value.
35
36 ## Sorting
37
38 As a contrived example, create a table of an image's dominant colors with the most dominant color first, and display the relative luminance of each dominant color:
39
40 ```go-html-template
41 {{ with resources.Get "images/a.jpg" }}
42 <table>
43 <thead>
44 <tr>
45 <th>Color</th>
46 <th>Relative luminance</th>
47 </tr>
48 </thead>
49 <tbody>
50 {{ range .Colors }}
51 <tr>
52 <td>{{ .ColorHex }}</td>
53 <td>{{ .Luminance | lang.FormatNumber 4 }}</td>
54 </tr>
55 {{ end }}
56 </tbody>
57 </table>
58 {{ end }}
59 ```
60
61 Hugo renders this to:
62
63 ColorHex|Relative luminance
64 :--|:--
65 `#bebebd`|`0.5145`
66 `#514947`|`0.0697`
67 `#768a9a`|`0.2436`
68 `#647789`|`0.1771`
69 `#90725e`|`0.1877`
70 `#a48974`|`0.2704`
71
72 To sort by dominance with the least dominant color first:
73
74 ```go-html-template
75 {{ range .Colors | collections.Reverse }}
76 ```
77
78 To sort by relative luminance with the darkest color first:
79
80 ```go-html-template
81 {{ range sort .Colors "Luminance" }}
82 ```
83
84 To sort by relative luminance with the lightest color first, use either of these constructs:
85
86 ```go-html-template
87 {{ range sort .Colors "Luminance" | collections.Reverse }}
88 {{ range sort .Colors "Luminance" "desc" }}
89 ```
90
91 ## Examples
92
93 ### Image borders
94
95 To add a 5 pixel border to an image using the most dominant color:
96
97 ```go-html-template
98 {{ with resources.Get "images/a.jpg" }}
99 {{ $mostDominant := index .Colors 0 }}
100 {{ $filter := images.Padding 5 $mostDominant }}
101 {{ with .Filter $filter }}
102 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
103 {{ end }}
104 {{ end }}
105 ```
106
107 To add a 5 pixel border to an image using the darkest dominant color:
108
109 ```go-html-template
110 {{ with resources.Get "images/a.jpg" }}
111 {{ $darkest := index (sort .Colors "Luminance") 0 }}
112 {{ $filter := images.Padding 5 $darkest }}
113 {{ with .Filter $filter }}
114 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
115 {{ end }}
116 {{ end }}
117 ```
118
119 ### Light text on dark background
120
121 To create a text box where the foreground and background colors are derived from an image's lightest and darkest dominant colors:
122
123 ```go-html-template
124 {{ with resources.Get "images/a.jpg" }}
125 {{ $darkest := index (sort .Colors "Luminance") 0 }}
126 {{ $lightest := index (sort .Colors "Luminance" "desc") 0 }}
127 <div style="background: {{ $darkest }};">
128 <div style="color: {{ $lightest }};">
129 <p>This is light text on a dark background.</p>
130 </div>
131 </div>
132 {{ end }}
133 ```
134
135 ### WCAG contrast ratio
136
137 In the previous example we placed light text on a dark background, but does this color combination conform to [WCAG][] guidelines for either the [minimum][] or the [enhanced][] contrast ratio?
138
139 The WCAG defines the [contrast ratio][] as:
140
141 $$contrast\ ratio = { L_1 + 0.05 \over L_2 + 0.05 }$$
142
143 where \(L_1\) is the relative luminance of the lightest color and \(L_2\) is the relative luminance of the darkest color.
144
145 Calculate the contrast ratio to determine WCAG conformance:
146
147 ```go-html-template
148 {{ with resources.Get "images/a.jpg" }}
149 {{ $lightest := index (sort .Colors "Luminance" "desc") 0 }}
150 {{ $darkest := index (sort .Colors "Luminance") 0 }}
151 {{ $cr := div
152 (add $lightest.Luminance 0.05)
153 (add $darkest.Luminance 0.05)
154 }}
155 {{ if ge $cr 7.5 }}
156 {{ printf "The %.2f contrast ratio conforms to WCAG Level AAA." $cr }}
157 {{ else if ge $cr 4.5 }}
158 {{ printf "The %.2f contrast ratio conforms to WCAG Level AA." $cr }}
159 {{ else }}
160 {{ printf "The %.2f contrast ratio does not conform to WCAG guidelines." $cr }}
161 {{ end }}
162 {{ end }}
163 ```
164
165 [WCAG]: https://en.wikipedia.org/wiki/Web_Content_Accessibility_Guidelines
166 [`images.Dither`]: /functions/images/dither/
167 [`images.Padding`]: /functions/images/padding/
168 [`images.Text`]: /functions/images/text/
169 [`reflect.IsImageResourceProcessable`]: /functions/reflect/isimageresourceprocessable/
170 [contrast ratio]: https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
171 [enhanced]: https://www.w3.org/WAI/WCAG22/quickref/?showtechniques=145#contrast-enhanced
172 [hexadecimal color]: https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
173 [minimum]: https://www.w3.org/WAI/WCAG22/quickref/?showtechniques=145#contrast-minimum
174 [relative luminance]: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance