github.com/neohugo/neohugo@v0.123.8/resources/images/color_test.go (about) 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 ) 22 23 func TestHexStringToColor(t *testing.T) { 24 c := qt.New(t) 25 26 for _, test := range []struct { 27 arg string 28 expect any 29 }{ 30 {"f", false}, 31 {"#f", false}, 32 {"#fffffff", false}, 33 {"fffffff", false}, 34 {"#fff", color.White}, 35 {"fff", color.White}, 36 {"FFF", color.White}, 37 {"FfF", color.White}, 38 {"#ffffff", color.White}, 39 {"ffffff", color.White}, 40 {"#000", color.Black}, 41 {"#4287f5", color.RGBA{R: 0x42, G: 0x87, B: 0xf5, A: 0xff}}, 42 {"777", color.RGBA{R: 0x77, G: 0x77, B: 0x77, A: 0xff}}, 43 } { 44 45 test := test 46 c.Run(test.arg, func(c *qt.C) { 47 c.Parallel() 48 49 result, err := hexStringToColor(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 75 test := test 76 c.Run(test.expect, func(c *qt.C) { 77 c.Parallel() 78 79 result := ColorToHexString(test.arg) 80 81 c.Assert(result, qt.Equals, test.expect) 82 }) 83 84 } 85 } 86 87 func TestAddColorToPalette(t *testing.T) { 88 c := qt.New(t) 89 90 palette := color.Palette{color.White, color.Black} 91 92 c.Assert(AddColorToPalette(color.White, palette), qt.HasLen, 2) 93 94 blue1, _ := hexStringToColor("34c3eb") 95 blue2, _ := hexStringToColor("34c3eb") 96 white, _ := hexStringToColor("fff") 97 98 c.Assert(AddColorToPalette(white, palette), qt.HasLen, 2) 99 c.Assert(AddColorToPalette(blue1, palette), qt.HasLen, 3) 100 c.Assert(AddColorToPalette(blue2, palette), qt.HasLen, 3) 101 } 102 103 func TestReplaceColorInPalette(t *testing.T) { 104 c := qt.New(t) 105 106 palette := color.Palette{color.White, color.Black} 107 offWhite, _ := hexStringToColor("fcfcfc") 108 109 ReplaceColorInPalette(offWhite, palette) 110 111 c.Assert(palette, qt.HasLen, 2) 112 c.Assert(palette[0], qt.Equals, offWhite) 113 }