github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/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 interface{}
    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 TestAddColorToPalette(t *testing.T) {
    64  	c := qt.New(t)
    65  
    66  	palette := color.Palette{color.White, color.Black}
    67  
    68  	c.Assert(AddColorToPalette(color.White, palette), qt.HasLen, 2)
    69  
    70  	blue1, _ := hexStringToColor("34c3eb")
    71  	blue2, _ := hexStringToColor("34c3eb")
    72  	white, _ := hexStringToColor("fff")
    73  
    74  	c.Assert(AddColorToPalette(white, palette), qt.HasLen, 2)
    75  	c.Assert(AddColorToPalette(blue1, palette), qt.HasLen, 3)
    76  	c.Assert(AddColorToPalette(blue2, palette), qt.HasLen, 3)
    77  }
    78  
    79  func TestReplaceColorInPalette(t *testing.T) {
    80  	c := qt.New(t)
    81  
    82  	palette := color.Palette{color.White, color.Black}
    83  	offWhite, _ := hexStringToColor("fcfcfc")
    84  
    85  	ReplaceColorInPalette(offWhite, palette)
    86  
    87  	c.Assert(palette, qt.HasLen, 2)
    88  	c.Assert(palette[0], qt.Equals, offWhite)
    89  }