Debian Codegraph @

resources/images/color_test.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 "image/color"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/htesting/hqt"
22 )
23
24 func TestHexStringToColor(t *testing.T) {
25 c := qt.New(t)
26
27 for _, test := range []struct {
28 arg string
29 expect any
30 }{
31 {"f", false},
32 {"#f", false},
33 {"#fffffff", false},
34 {"fffffff", false},
35 {"#fff", color.White},
36 {"fff", color.White},
37 {"FFF", color.White},
38 {"FfF", color.White},
39 {"#ffffff", color.White},
40 {"ffffff", color.White},
41 {"#000", color.Black},
42 {"#4287f5", color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}},
43 {"777", color.NRGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}},
44 // Hex notation is not alpha-premultiplied, so a transparent color
45 // must parse to a color.NRGBA. See issue 12536.
46 {"#00f7", color.NRGBA{R: 0x00, G: 0x00, B: 0xff, A: 0x77}},
47 {"4287f580", color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}},
48 } {
49
50 c.Run(test.arg, func(c *qt.C) {
51 c.Parallel()
52
53 result, err := hexStringToColorGo(test.arg)
54
55 if b, ok := test.expect.(bool); ok && !b {
56 c.Assert(err, qt.Not(qt.IsNil))
57 return
58 }
59
60 c.Assert(err, qt.IsNil)
61 c.Assert(result, qt.DeepEquals, test.expect)
62 })
63
64 }
65 }
66
67 func TestColorToHexString(t *testing.T) {
68 c := qt.New(t)
69
70 for _, test := range []struct {
71 arg color.Color
72 expect string
73 }{
74 {color.White, "#ffffff"},
75 {color.Black, "#000000"},
76 {color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}, "#4287f5"},
77
78 // 50% opacity. Hex notation is not alpha-premultiplied, so the
79 // components of a transparent color are the non-premultiplied ones.
80 // Note that the .Colors (dominant colors) received from the Image resource
81 // will always have an alpha value of 0xff.
82 {color.NRGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"},
83 } {
84
85 c.Run(test.expect, func(c *qt.C) {
86 c.Parallel()
87
88 result := ColorGoToHexString(test.arg)
89
90 c.Assert(result, qt.Equals, test.expect)
91 })
92
93 }
94 }
95
96 func TestAddColorToPalette(t *testing.T) {
97 c := qt.New(t)
98
99 palette := color.Palette{color.White, color.Black}
100
101 c.Assert(AddColorToPalette(color.White, palette), qt.HasLen, 2)
102
103 blue1, _ := hexStringToColorGo("34c3eb")
104 blue2, _ := hexStringToColorGo("34c3eb")
105 white, _ := hexStringToColorGo("fff")
106
107 c.Assert(AddColorToPalette(white, palette), qt.HasLen, 2)
108 c.Assert(AddColorToPalette(blue1, palette), qt.HasLen, 3)
109 c.Assert(AddColorToPalette(blue2, palette), qt.HasLen, 3)
110 }
111
112 func TestReplaceColorInPalette(t *testing.T) {
113 c := qt.New(t)
114
115 palette := color.Palette{color.White, color.Black}
116 offWhite, _ := hexStringToColorGo("fcfcfc")
117
118 ReplaceColorInPalette(offWhite, palette)
119
120 c.Assert(palette, qt.HasLen, 2)
121 c.Assert(palette[0], qt.Equals, offWhite)
122 }
123
124 func TestColorLuminance(t *testing.T) {
125 c := qt.New(t)
126 c.Assert(hexStringToColor("#000000").Luminance(), hqt.IsSameFloat64, 0.0)
127 c.Assert(hexStringToColor("#768a9a").Luminance(), hqt.IsSameFloat64, 0.24361603589088263)
128 c.Assert(hexStringToColor("#d5bc9f").Luminance(), hqt.IsSameFloat64, 0.5261577672685374)
129 c.Assert(hexStringToColor("#ffffff").Luminance(), hqt.IsSameFloat64, 1.0)
130 }