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.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}},
43 {"777", color.RGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}},
44 } {
45
46 c.Run(test.arg, func(c *qt.C) {
47 c.Parallel()
48
49 result, err := hexStringToColorGo(test.arg)
50
51 if b, ok := test.expect.(bool); ok && !b {
52 c.Assert(err, qt.Not(qt.IsNil))
53 return
54 }
55
56 c.Assert(err, qt.IsNil)
57 c.Assert(result, qt.DeepEquals, test.expect)
58 })
59
60 }
61 }
62
63 func TestColorToHexString(t *testing.T) {
64 c := qt.New(t)
65
66 for _, test := range []struct {
67 arg color.Color
68 expect string
69 }{
70 {color.White, "#ffffff"},
71 {color.Black, "#000000"},
72 {color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}, "#4287f5"},
73
74 // 50% opacity.
75 // Note that the .Colors (dominant colors) received from the Image resource
76 // will always have an alpha value of 0xff.
77 {color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0x80}, "#4287f580"},
78 } {
79
80 c.Run(test.expect, func(c *qt.C) {
81 c.Parallel()
82
83 result := ColorGoToHexString(test.arg)
84
85 c.Assert(result, qt.Equals, test.expect)
86 })
87
88 }
89 }
90
91 func TestAddColorToPalette(t *testing.T) {
92 c := qt.New(t)
93
94 palette := color.Palette{color.White, color.Black}
95
96 c.Assert(AddColorToPalette(color.White, palette), qt.HasLen, 2)
97
98 blue1, _ := hexStringToColorGo("34c3eb")
99 blue2, _ := hexStringToColorGo("34c3eb")
100 white, _ := hexStringToColorGo("fff")
101
102 c.Assert(AddColorToPalette(white, palette), qt.HasLen, 2)
103 c.Assert(AddColorToPalette(blue1, palette), qt.HasLen, 3)
104 c.Assert(AddColorToPalette(blue2, palette), qt.HasLen, 3)
105 }
106
107 func TestReplaceColorInPalette(t *testing.T) {
108 c := qt.New(t)
109
110 palette := color.Palette{color.White, color.Black}
111 offWhite, _ := hexStringToColorGo("fcfcfc")
112
113 ReplaceColorInPalette(offWhite, palette)
114
115 c.Assert(palette, qt.HasLen, 2)
116 c.Assert(palette[0], qt.Equals, offWhite)
117 }
118
119 func TestColorLuminance(t *testing.T) {
120 c := qt.New(t)
121 c.Assert(hexStringToColor("#000000").Luminance(), hqt.IsSameFloat64, 0.0)
122 c.Assert(hexStringToColor("#768a9a").Luminance(), hqt.IsSameFloat64, 0.24361603589088263)
123 c.Assert(hexStringToColor("#d5bc9f").Luminance(), hqt.IsSameFloat64, 0.5261577672685374)
124 c.Assert(hexStringToColor("#ffffff").Luminance(), hqt.IsSameFloat64, 1.0)
125 }