Debian Codegraph @

vendor/github.com/lucasb-eyer/go-colorful/README.md

raw
1 go-colorful
2 ===========
3
4 [![Go Reference](https://pkg.go.dev/badge/github.com/lucasb-eyer/go-colorful.svg)](https://pkg.go.dev/github.com/lucasb-eyer/go-colorful)
5 [![go reportcard](https://goreportcard.com/badge/github.com/lucasb-eyer/go-colorful)](https://goreportcard.com/report/github.com/lucasb-eyer/go-colorful)
6
7 A library for playing with colors in Go. Supports Go 1.13 onwards.
8
9 Why?
10 ====
11 I love games. I make games. I love detail and I get lost in detail.
12 One such detail popped up during the development of [Memory Which Does Not Suck](https://github.com/lucasb-eyer/mwdns/),
13 when we wanted the server to assign the players random colors. Sometimes
14 two players got very similar colors, which bugged me. The very same evening,
15 [I want hue](http://tools.medialab.sciences-po.fr/iwanthue/) was the top post
16 on HackerNews' frontpage and showed me how to Do It Right™. Last but not
17 least, there was no library for handling color spaces available in go. Colorful
18 does just that and implements Go's `color.Color` interface.
19
20 What?
21 =====
22 Go-Colorful stores colors in RGB and provides methods from converting these to various color-spaces. Currently supported colorspaces are:
23
24 - **RGB:** All three of Red, Green and Blue in [0..1].
25 - **HSL:** Hue in [0..360], Saturation and Luminance in [0..1]. For legacy reasons; please forget that it exists.
26 - **HSV:** Hue in [0..360], Saturation and Value in [0..1]. You're better off using HCL, see below.
27 - **Hex RGB:** The "internet" color format, as in #FF00FF.
28 - **Linear RGB:** See [gamma correct rendering](http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/).
29 - **CIE-XYZ:** CIE's standard color space, almost in [0..1].
30 - **CIE-xyY:** encodes chromacity in x and y and luminance in Y, all in [0..1]
31 - **CIE-L\*a\*b\*:** A *perceptually uniform* color space, i.e. distances are meaningful. L\* in [0..1] and a\*, b\* almost in [-1..1].
32 - **CIE-L\*u\*v\*:** Very similar to CIE-L\*a\*b\*, there is [no consensus](http://en.wikipedia.org/wiki/CIELUV#Historical_background) on which one is "better".
33 - **CIE-L\*C\*h° (HCL):** This is generally the [most useful](http://vis4.net/blog/posts/avoid-equidistant-hsv-colors/) one; CIE-L\*a\*b\* space in polar coordinates, i.e. a *better* HSV. H° is in [0..360], C\* almost in [0..1] and L\* as in CIE-L\*a\*b\*.
34 - **CIE LCh(uv):** Called `LuvLCh` in code, this is a cylindrical transformation of the CIE-L\*u\*v\* color space. Like HCL above: H° is in [0..360], C\* almost in [0..1] and L\* as in CIE-L\*u\*v\*.
35 - **HSLuv:** The better alternative to HSL, see [here](https://www.hsluv.org/) and [here](https://www.kuon.ch/post/2020-03-08-hsluv/). Hue in [0..360], Saturation and Luminance in [0..1].
36 - **HPLuv:** A variant of HSLuv. The color space is smoother, but only pastel colors can be included. Because the valid colors are limited, it's easy to get invalid Saturation values way above 1.0, indicating the color can't be represented in HPLuv because it's not pastel.
37 - **Oklab:** A perceptual color space by Björn Ottosson that improves on CIE-L\*a\*b\* with better perceptual uniformity, especially for blue hues. L in [0..1], a and b roughly in [-0.5..0.5]. See [Oklab](https://bottosson.github.io/posts/oklab/).
38 - **Oklch:** The cylindrical (polar) representation of Oklab, similar to HCL. L in [0..1], C roughly in [0..0.5], h° in [0..360].
39
40 For the colorspaces where it makes sense (XYZ, Lab, Luv, HCl), the
41 [D65](http://en.wikipedia.org/wiki/Illuminant_D65) is used as reference white
42 by default but methods for using your own reference white are provided.
43
44 A coordinate being *almost in* a range means that generally it is, but for very
45 bright colors and depending on the reference white, it might overflow this
46 range slightly. For example, C\* of #0000ff is 1.338.
47
48 Unit-tests are provided.
49
50 Nice, but what's it useful for?
51 -------------------------------
52
53 - Converting color spaces. Some people like to do that.
54 - Blending (interpolating) between colors in a "natural" look by using the right colorspace.
55 - Generating random colors under some constraints (e.g. colors of the same shade, or shades of one color.)
56 - Generating gorgeous random palettes with distinct colors of a same temperature.
57
58 So which colorspace should I use?
59 =================================
60 It depends on what you want to do. I think the folks from *I want hue* are
61 on-spot when they say that RGB fits to how *screens produce* color, CIE L\*a\*b\*
62 fits how *humans perceive* color and HCL fits how *humans think* colors.
63
64 Whenever you'd use HSV, rather go for CIE-L\*C\*h°. for fixed lightness L\* and
65 chroma C\* values, the hue angle h° rotates through colors of the same
66 perceived brightness and intensity.
67
68 How?
69 ====
70
71 ### Installing
72 Installing the library is as easy as
73
74 ```bash
75 $ go get github.com/lucasb-eyer/go-colorful
76 ```
77
78 The package can then be used through an
79
80 ```go
81 import "github.com/lucasb-eyer/go-colorful"
82 ```
83
84 ### Basic usage
85
86 Create a beautiful blue color using different source space:
87
88 ```go
89 // Any of the following should be the same
90 c := colorful.Color{0.313725, 0.478431, 0.721569}
91 c, err := colorful.Hex("#517AB8")
92 if err != nil {
93 log.Fatal(err)
94 }
95 c = colorful.Hsv(216.0, 0.56, 0.722)
96 c = colorful.Xyz(0.189165, 0.190837, 0.480248)
97 c = colorful.Xyy(0.219895, 0.221839, 0.190837)
98 c = colorful.Lab(0.507850, 0.040585,-0.370945)
99 c = colorful.Luv(0.507849,-0.194172,-0.567924)
100 c = colorful.Hcl(276.2440, 0.373160, 0.507849)
101 c = colorful.OkLab(0.577227, -0.021391, -0.104541)
102 c = colorful.OkLch(0.577227, 0.106707, 258.435657)
103 fmt.Printf("RGB values: %v, %v, %v", c.R, c.G, c.B)
104 ```
105
106 And then converting this color back into various color spaces:
107
108 ```go
109 hex := c.Hex()
110 h, s, v := c.Hsv()
111 x, y, z := c.Xyz()
112 x, y, Y := c.Xyy()
113 l, a, b := c.Lab()
114 l, u, v := c.Luv()
115 h, c, l := c.Hcl()
116 l, a, b = c.OkLab()
117 l, c, h = c.OkLch()
118 ```
119
120 Note that, because of Go's unfortunate choice of requiring an initial uppercase,
121 the name of the functions relating to the xyY space are just off. If you have
122 any good suggestion, please open an issue. (I don't consider XyY good.)
123
124 ### The `color.Color` interface
125 Because a `colorful.Color` implements Go's `color.Color` interface (found in the
126 `image/color` package), it can be used anywhere that expects a `color.Color`.
127
128 Furthermore, you can convert anything that implements the `color.Color` interface
129 into a `colorful.Color` using the `MakeColor` function:
130
131 ```go
132 c, ok := colorful.MakeColor(color.Gray16{12345})
133 ```
134
135 **Caveat:** Be aware that this latter conversion (using `MakeColor`) hits a
136 corner-case when alpha is exactly zero. Because `color.Color` uses pre-multiplied
137 alpha colors, this means the RGB values are lost (set to 0) and it's impossible
138 to recover them. In such a case `MakeColor` will return `false` as its second value.
139
140 ### Comparing colors
141 In the RGB color space, the Euclidean distance between colors *doesn't* correspond
142 to visual/perceptual distance. This means that two pairs of colors which have the
143 same distance in RGB space can look much further apart. This is fixed by the
144 CIE-L\*a\*b\*, CIE-L\*u\*v\* and CIE-L\*C\*h° color spaces.
145 Thus you should only compare colors in any of these space.
146 (Note that the distance in CIE-L\*a\*b\* and CIE-L\*C\*h° are the same, since it's the same space but in cylindrical coordinates)
147
148 ![Color distance comparison](doc/colordist/colordist.png)
149
150 The two colors shown on the top look much more different than the two shown on
151 the bottom. Still, in RGB space, their distance is the same.
152 Here is a little example program which shows the distances between the top two
153 and bottom two colors in RGB, CIE-L\*a\*b\* and CIE-L\*u\*v\* space. You can find it in `doc/colordist/colordist.go`.
154
155 ```go
156 package main
157
158 import "fmt"
159 import "github.com/lucasb-eyer/go-colorful"
160
161 func main() {
162 c1a := colorful.Color{150.0 / 255.0, 10.0 / 255.0, 150.0 / 255.0}
163 c1b := colorful.Color{53.0 / 255.0, 10.0 / 255.0, 150.0 / 255.0}
164 c2a := colorful.Color{10.0 / 255.0, 150.0 / 255.0, 50.0 / 255.0}
165 c2b := colorful.Color{99.9 / 255.0, 150.0 / 255.0, 10.0 / 255.0}
166
167 fmt.Printf("DistanceRgb: c1: %v\tand c2: %v\n", c1a.DistanceRgb(c1b), c2a.DistanceRgb(c2b))
168 fmt.Printf("DistanceLab: c1: %v\tand c2: %v\n", c1a.DistanceLab(c1b), c2a.DistanceLab(c2b))
169 fmt.Printf("DistanceLuv: c1: %v\tand c2: %v\n", c1a.DistanceLuv(c1b), c2a.DistanceLuv(c2b))
170 fmt.Printf("DistanceCIE76: c1: %v\tand c2: %v\n", c1a.DistanceCIE76(c1b), c2a.DistanceCIE76(c2b))
171 fmt.Printf("DistanceCIE94: c1: %v\tand c2: %v\n", c1a.DistanceCIE94(c1b), c2a.DistanceCIE94(c2b))
172 fmt.Printf("DistanceCIEDE2000: c1: %v\tand c2: %v\n", c1a.DistanceCIEDE2000(c1b), c2a.DistanceCIEDE2000(c2b))
173 }
174 ```
175
176 Running the above program shows that you should always prefer any of the CIE distances:
177
178 ```bash
179 $ go run colordist.go
180 DistanceRgb: c1: 0.3803921568627451 and c2: 0.3858713931171159
181 DistanceLab: c1: 0.32048458312798056 and c2: 0.24397151758565272
182 DistanceLuv: c1: 0.5134369614199698 and c2: 0.2568692839860636
183 DistanceCIE76: c1: 0.32048458312798056 and c2: 0.24397151758565272
184 DistanceCIE94: c1: 0.19799168128511324 and c2: 0.12207136371167401
185 DistanceCIEDE2000: c1: 0.17274551120971166 and c2: 0.10665210031428465
186 ```
187
188 It also shows that `DistanceLab` is more formally known as `DistanceCIE76` and
189 has been superseded by the slightly more accurate, but much more expensive
190 `DistanceCIE94` and `DistanceCIEDE2000`.
191
192 Note that `AlmostEqualRgb` is provided mainly for (unit-)testing purposes. Use
193 it only if you really know what you're doing. It will eat your cat.
194
195 ### Blending colors
196 Blending is highly connected to distance, since it basically "walks through" the
197 colorspace thus, if the colorspace maps distances well, the walk is "smooth".
198
199 Colorful comes with blending functions in RGB, HSV, Oklab, Oklch, and any of the CIE-LAB spaces.
200 Of course, you'd rather want to use the blending functions of the LAB spaces since
201 these spaces map distances well but, just in case, here is an example showing
202 you how the blendings (`#fdffcc` to `#242a42`) are done in the various spaces:
203
204 ![Blending colors in different spaces.](doc/colorblend/colorblend.png)
205
206 What you see is that HSV is really bad: it adds some green, which is not present
207 in the original colors at all! RGB is much better, but it stays light a little
208 too long. LUV and LAB both hit the right lightness but LAB has a little more
209 color. HCL works in the same vein as HSV (both cylindrical interpolations) but
210 it does it right in that there is no green appearing and the lightness changes
211 in a linear manner.
212
213 While this seems all good, you need to know one thing: When interpolating in any
214 of the CIE color spaces, you might get invalid RGB colors! This is important if
215 the starting and ending colors are user-input or random. An example of where this
216 happens is when blending between `#eeef61` and `#1e3140`:
217
218 ![Invalid RGB colors may crop up when blending in CIE spaces.](doc/colorblend/invalid.png)
219
220 You can test whether a color is a valid RGB color by calling the `IsValid` method
221 and indeed, calling IsValid will return false for the redish colors on the bottom.
222 One way to "fix" this is to get a valid color close to the invalid one by calling
223 `Clamped`, which always returns a nearby valid color. Doing this, we get the
224 following result, which is satisfactory:
225
226 ![Fixing invalid RGB colors by clamping them to the valid range.](doc/colorblend/clamped.png)
227
228 The following is the code creating the above three images; it can be found in `doc/colorblend/colorblend.go`
229
230 ```go
231 package main
232
233 import "fmt"
234 import "github.com/lucasb-eyer/go-colorful"
235 import "image"
236 import "image/draw"
237 import "image/png"
238 import "os"
239
240 func main() {
241 blocks := 10
242 blockw := 40
243 img := image.NewRGBA(image.Rect(0,0,blocks*blockw,200))
244
245 c1, _ := colorful.Hex("#fdffcc")
246 c2, _ := colorful.Hex("#242a42")
247
248 // Use these colors to get invalid RGB in the gradient.
249 //c1, _ := colorful.Hex("#EEEF61")
250 //c2, _ := colorful.Hex("#1E3140")
251
252 for i := 0 ; i < blocks ; i++ {
253 draw.Draw(img, image.Rect(i*blockw, 0,(i+1)*blockw, 40), &image.Uniform{c1.BlendHsv(c2, float64(i)/float64(blocks-1))}, image.Point{}, draw.Src)
254 draw.Draw(img, image.Rect(i*blockw, 40,(i+1)*blockw, 80), &image.Uniform{c1.BlendLuv(c2, float64(i)/float64(blocks-1))}, image.Point{}, draw.Src)
255 draw.Draw(img, image.Rect(i*blockw, 80,(i+1)*blockw,120), &image.Uniform{c1.BlendRgb(c2, float64(i)/float64(blocks-1))}, image.Point{}, draw.Src)
256 draw.Draw(img, image.Rect(i*blockw,120,(i+1)*blockw,160), &image.Uniform{c1.BlendLab(c2, float64(i)/float64(blocks-1))}, image.Point{}, draw.Src)
257 draw.Draw(img, image.Rect(i*blockw,160,(i+1)*blockw,200), &image.Uniform{c1.BlendHcl(c2, float64(i)/float64(blocks-1))}, image.Point{}, draw.Src)
258
259 // This can be used to "fix" invalid colors in the gradient.
260 //draw.Draw(img, image.Rect(i*blockw,160,(i+1)*blockw,200), &image.Uniform{c1.BlendHcl(c2, float64(i)/float64(blocks-1)).Clamped()}, image.Point{}, draw.Src)
261 }
262
263 toimg, err := os.Create("colorblend.png")
264 if err != nil {
265 fmt.Printf("Error: %v", err)
266 return
267 }
268 defer toimg.Close()
269
270 png.Encode(toimg, img)
271 }
272 ```
273
274 #### Generating color gradients
275 A very common reason to blend colors is creating gradients. There is an example
276 program in [doc/gradientgen.go](doc/gradientgen/gradientgen.go); it doesn't use any API
277 which hasn't been used in the previous example code, so I won't bother pasting
278 the code in here. Just look at that gorgeous gradient it generated in HCL space:
279
280 !["Spectral" colorbrewer gradient in HCL space.](doc/gradientgen/gradientgen.png)
281
282 ### Getting random colors
283 It is sometimes necessary to generate random colors. You could simply do this
284 on your own by generating colors with random values. By restricting the random
285 values to a range smaller than [0..1] and using a space such as CIE-H\*C\*l° or
286 HSV, you can generate both random shades of a color or random colors of a
287 lightness:
288
289 ```go
290 random_blue := colorful.Hcl(180.0+rand.Float64()*50.0, 0.2+rand.Float64()*0.8, 0.3+rand.Float64()*0.7)
291 random_dark := colorful.Hcl(rand.Float64()*360.0, rand.Float64(), rand.Float64()*0.4)
292 random_light := colorful.Hcl(rand.Float64()*360.0, rand.Float64(), 0.6+rand.Float64()*0.4)
293 ```
294
295 Since getting random "warm" and "happy" colors is quite a common task, there
296 are some helper functions:
297
298 ```go
299 colorful.WarmColor()
300 colorful.HappyColor()
301 colorful.FastWarmColor()
302 colorful.FastHappyColor()
303 ```
304
305 The ones prefixed by `Fast` are faster but less coherent since they use the HSV
306 space as opposed to the regular ones which use CIE-L\*C\*h° space. The
307 following picture shows the warm colors in the top two rows and happy colors
308 in the bottom two rows. Within these, the first is the regular one and the
309 second is the fast one.
310
311 ![Warm, fast warm, happy and fast happy random colors, respectively.](doc/colorgens/colorgens.png)
312
313 Don't forget to initialize the random seed! You can see the code used for
314 generating this picture in `doc/colorgens/colorgens.go`.
315
316 ### Getting random palettes
317 As soon as you need to generate more than one random color, you probably want
318 them to be distinguishable. Playing against an opponent which has almost the
319 same blue as I do is not fun. This is where random palettes can help.
320
321 These palettes are generated using an algorithm which ensures that all colors
322 on the palette are as distinguishable as possible. Again, there is a `Fast`
323 method which works in HSV and is less perceptually uniform and a non-`Fast`
324 method which works in CIE spaces. For more theory on `SoftPalette`, check out
325 [I want hue](http://tools.medialab.sciences-po.fr/iwanthue/theory.php). Yet
326 again, there is a `Happy` and a `Warm` version, which do what you expect, but
327 now there is an additional `Soft` version, which is more configurable: you can
328 give a constraint on the color space in order to get colors within a certain *feel*.
329
330 Let's start with the simple methods first, all they take is the amount of
331 colors to generate, which could, for example, be the player count. They return
332 an array of `colorful.Color` objects:
333
334 ```go
335 pal1, err1 := colorful.WarmPalette(10)
336 pal2 := colorful.FastWarmPalette(10)
337 pal3, err3 := colorful.HappyPalette(10)
338 pal4 := colorful.FastHappyPalette(10)
339 pal5, err5 := colorful.SoftPalette(10)
340 ```
341
342 Note that the non-fast methods *may* fail if you ask for way too many colors.
343 Let's move on to the advanced one, namely `SoftPaletteEx`. Besides the color
344 count, this function takes a `SoftPaletteSettings` object as argument. The
345 interesting part here is its `CheckColor` member, which is a boolean function
346 taking three floating points as arguments: `l`, `a` and `b`. This function
347 should return `true` for colors which lie within the region you want and `false`
348 otherwise. The other members are `Iteration`, which should be within [5..100]
349 where higher means slower but more exact palette, and `ManySamples` which you
350 should set to `true` in case your `CheckColor` constraint rejects a large part
351 of the color space.
352
353 For example, to create a palette of 10 brownish colors, you'd call it like this:
354
355 ```go
356 func isbrowny(l, a, b float64) bool {
357 h, c, L := colorful.LabToHcl(l, a, b)
358 return 10.0 < h && h < 50.0 && 0.1 < c && c < 0.5 && L < 0.5
359 }
360 // Since the above function is pretty restrictive, we set ManySamples to true.
361 brownies := colorful.SoftPaletteEx(10, colorful.SoftPaletteSettings{isbrowny, 50, true})
362 ```
363
364 The following picture shows the palettes generated by all of these methods
365 (sourcecode in `doc/palettegens/palettegens.go`), in the order they were presented, i.e.
366 from top to bottom: `Warm`, `FastWarm`, `Happy`, `FastHappy`, `Soft`,
367 `SoftEx(isbrowny)`. All of them contain some randomness, so YMMV.
368
369 ![All example palettes](doc/palettegens/palettegens.png)
370
371 Again, the code used for generating the above image is available as [doc/palettegens/palettegens.go](https://github.com/lucasb-eyer/go-colorful/blob/master/doc/palettegens/palettegens.go).
372
373 ### Sorting colors
374
375 Sorting colors is not a well-defined operation. For example, {dark blue, dark red, light blue, light red} is already sorted if darker colors should precede lighter colors but would need to be re-sorted as {dark red, light red, dark blue, light blue} if longer-wavelength colors should precede shorter-wavelength colors.
376
377 Go-Colorful's `Sorted` function orders a list of colors so as to minimize the average distance between adjacent colors, including between the last and the first. (`Sorted` does not necessarily find the true minimum, only a reasonably close approximation.) The following picture, drawn by [doc/colorsort/colorsort.go](https://github.com/lucasb-eyer/go-colorful/blob/master/doc/colorsort/colorsort.go), illustrates `Sorted`'s behavior:
378
379 ![Sorting colors](doc/colorsort/colorsort.png)
380
381 The first row represents the input: a slice of 512 randomly chosen colors. The second row shows the colors sorted in CIE-L\*C\*h° space, ordered first by lightness (L), then by hue angle (h), and finally by chroma (C). Note that distracting pinstripes permeate the colors. Sorting using *any* color space and *any* ordering of the channels yields a similar pinstriped pattern. The third row of the image was sorted using Go-Colorful's `Sorted` function. Although the colors do not appear to be in any particular order, the sequence at least appears smoother than the one sorted by channel.
382
383
384 ### Using linear RGB for computations
385 There are two methods for transforming RGB⟷Linear RGB: a fast and almost precise one,
386 and a slow and precise one.
387
388 ```go
389 r, g, b := colorful.Hex("#FF0000").FastLinearRgb()
390 ```
391
392 TODO: describe some more.
393
394 ### Want to use some other reference point?
395
396 ```go
397 c := colorful.LabWhiteRef(0.507850, 0.040585,-0.370945, colorful.D50)
398 l, a, b := c.LabWhiteRef(colorful.D50)
399 ```
400
401 ### Reading and writing colors from databases
402
403 The type `HexColor` makes it easy to store colors as strings in a database. It
404 implements the [https://godoc.org/database/sql#Scanner](database/sql.Scanner)
405 and [database/sql/driver.Value](https://godoc.org/database/sql/driver.Value)
406 interfaces which provide automatic type conversion.
407
408 Example:
409
410 ```go
411 var hc HexColor
412 _, err := db.QueryRow("SELECT '#ff0000';").Scan(&hc)
413 // hc == HexColor{R: 1, G: 0, B: 0}; err == nil
414 ```
415
416 FAQ
417 ===
418
419 ### Q: I get all f!@#ed up values! Your library sucks!
420 A: You probably provided values in the wrong range. For example, RGB values are
421 expected to reside between 0 and 1, *not* between 0 and 255. Normalize your colors.
422
423 ### Q: Lab/Luv/HCl seem broken! Your library sucks!
424 They look like this:
425
426 <img height="150" src="https://user-images.githubusercontent.com/3779568/28646900-6548040c-7264-11e7-8f12-81097a97c260.png">
427
428 A: You're likely trying to generate and display colors that can't be represented by RGB,
429 and thus monitors. When you're trying to convert, say, `HCL(190.0, 1.0, 1.0).RGB255()`,
430 you're asking for RGB values of `(-2105.254 300.680 286.185)`, which clearly don't exist,
431 and the `RGB255` function just casts these numbers to `uint8`, creating wrap-around and
432 what looks like a completely broken gradient. What you want to do, is either use more
433 reasonable values of colors which actually exist in RGB, or just `Clamp()` the resulting
434 color to its nearest existing one, living with the consequences:
435 `HCL(190.0, 1.0, 1.0).Clamp().RGB255()`. It will look something like this:
436
437 <img height="150" src="https://user-images.githubusercontent.com/1476029/29596343-9a8c62c6-8771-11e7-9026-b8eb8852cc4a.png">
438
439 [Here's an issue going in-depth about this](https://github.com/lucasb-eyer/go-colorful/issues/14),
440 as well as [my answer](https://github.com/lucasb-eyer/go-colorful/issues/14#issuecomment-324205385),
441 both with code and pretty pictures. Also note that this was somewhat covered above in the
442 ["Blending colors" section](https://github.com/lucasb-eyer/go-colorful#blending-colors).
443
444 ### Q: In a tight loop, conversion to Lab/Luv/HCl/... are slooooow!
445 A: Yes, they are.
446 This library aims for correctness, readability, and modularity; it wasn't written with speed in mind.
447 A large part of the slowness comes from these conversions going through `LinearRgb` which uses powers.
448 I implemented a fast approximation to `LinearRgb` called `FastLinearRgb` by using Taylor approximations.
449 The approximation is roughly 5x faster and precise up to roughly 0.5%,
450 the major caveat being that if the input values are outside the range 0-1, accuracy drops dramatically.
451 You can use these in your conversions as follows:
452
453 ```go
454 col := // Get your color somehow
455 l, a, b := XyzToLab(LinearRgbToXyz(col.LinearRgb()))
456 ```
457
458 If you need faster versions of `Distance*` and `Blend*` that make use of this fast approximation,
459 feel free to implement them and open a pull-request, I'll happily accept.
460
461 The derivation of these functions can be followed in [this Jupyter notebook](doc/LinearRGB Approximations.ipynb).
462 Here's the main figure showing the approximation quality:
463
464 ![approximation quality](doc/approx-quality.png)
465
466 More speed could be gained by using SIMD instructions in many places.
467 You can also get more speed for specific conversions by approximating the full conversion function,
468 but that is outside the scope of this library.
469 Thanks to [@ZirconiumX](https://github.com/ZirconiumX) for starting this investigation,
470 see [issue #18](https://github.com/lucasb-eyer/go-colorful/issues/18) for details.
471
472 ### Q: Why would `MakeColor` ever fail!?
473 A: `MakeColor` fails when the alpha channel is zero. In that case, the
474 conversion is undefined. See [issue 21](https://github.com/lucasb-eyer/go-colorful/issues/21)
475 as well as the short caveat note in the ["The `color.Color` interface"](README.md#the-colorcolor-interface)
476 section above.
477
478 Who?
479 ====
480
481 This library was originally developed by Lucas Beyer, with notable
482 contributions from Bastien Dejean (@baskerville), Phil Kulak (@pkulak),
483 Christian Muehlhaeuser (@muesli), Scott Pakin (@spakin), and many others.
484 See the [contributors list](https://github.com/lucasb-eyer/go-colorful/graphs/contributors) for the full roster.
485
486 It is currently maintained by makeworld (@makew0rld).
487
488 ## License
489
490 This repo is under the MIT license, see [LICENSE](LICENSE) for details.