Debian Codegraph @

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

raw
1 ---
2 title: Colors
3 description: Returns a slice of an image's most dominant colors, ordered from most to least dominant, 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 The following examples use an image's dominant colors to style page elements.
94
95 ### Image borders
96
97 To add a 5 pixel border to an image using the most dominant color:
98
99 ```go-html-template
100 {{ with resources.Get "images/a.jpg" }}
101 {{ $mostDominant := index .Colors 0 }}
102 {{ $filter := images.Padding 5 $mostDominant }}
103 {{ with .Filter $filter }}
104 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
105 {{ end }}
106 {{ end }}
107 ```
108
109 To add a 5 pixel border to an image using the darkest dominant color:
110
111 ```go-html-template
112 {{ with resources.Get "images/a.jpg" }}
113 {{ $darkest := index (sort .Colors "Luminance") 0 }}
114 {{ $filter := images.Padding 5 $darkest }}
115 {{ with .Filter $filter }}
116 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
117 {{ end }}
118 {{ end }}
119 ```
120
121 ### Light text on dark background
122
123 To create a text box where the foreground and background colors are derived from an image's lightest and darkest dominant colors:
124
125 ```go-html-template
126 {{ with resources.Get "images/a.jpg" }}
127 {{ $darkest := index (sort .Colors "Luminance") 0 }}
128 {{ $lightest := index (sort .Colors "Luminance" "desc") 0 }}
129 <div style="background: {{ $darkest }};">
130 <div style="color: {{ $lightest }};">
131 <p>This is light text on a dark background.</p>
132 </div>
133 </div>
134 {{ end }}
135 ```
136
137 ### WCAG contrast ratio
138
139 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?
140
141 The WCAG defines the [contrast ratio][] as:
142
143 $$contrast\ ratio = { L_1 + 0.05 \over L_2 + 0.05 }$$
144
145 where \(L_1\) is the relative luminance of the lightest color and \(L_2\) is the relative luminance of the darkest color.
146
147 Calculate the contrast ratio to determine WCAG conformance:
148
149 ```go-html-template
150 {{ with resources.Get "images/a.jpg" }}
151 {{ $lightest := index (sort .Colors "Luminance" "desc") 0 }}
152 {{ $darkest := index (sort .Colors "Luminance") 0 }}
153 {{ $cr := div
154 (add $lightest.Luminance 0.05)
155 (add $darkest.Luminance 0.05)
156 }}
157 {{ if ge $cr 7.5 }}
158 {{ printf "The %.2f contrast ratio conforms to WCAG Level AAA." $cr }}
159 {{ else if ge $cr 4.5 }}
160 {{ printf "The %.2f contrast ratio conforms to WCAG Level AA." $cr }}
161 {{ else }}
162 {{ printf "The %.2f contrast ratio does not conform to WCAG guidelines." $cr }}
163 {{ end }}
164 {{ end }}
165 ```
166
167 [WCAG]: https://en.wikipedia.org/wiki/Web_Content_Accessibility_Guidelines
168 [`images.Dither`]: /functions/images/dither/
169 [`images.Padding`]: /functions/images/padding/
170 [`images.Text`]: /functions/images/text/
171 [`reflect.IsImageResourceProcessable`]: /functions/reflect/isimageresourceprocessable/
172 [contrast ratio]: https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
173 [enhanced]: https://www.w3.org/WAI/WCAG22/quickref/?showtechniques=145#contrast-enhanced
174 [hexadecimal color]: https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color
175 [minimum]: https://www.w3.org/WAI/WCAG22/quickref/?showtechniques=145#contrast-minimum
176 [relative luminance]: https://www.w3.org/TR/WCAG21/#dfn-relative-luminance