github.com/ericwq/aprilsh@v0.0.0-20240517091432-958bc568daa0/terminal/color_test.go (about) 1 // Copyright 2022 wangqi. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package terminal 6 7 // Copyright 2018 The TCell Authors 8 // 9 // Licensed under the Apache License, Version 2.0 (the "License"); 10 // you may not use file except in compliance with the License. 11 // You may obtain a copy of the license at 12 // 13 // http://www.apache.org/licenses/LICENSE-2.0 14 // 15 // Unless required by applicable law or agreed to in writing, software 16 // distributed under the License is distributed on an "AS IS" BASIS, 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 // See the License for the specific language governing permissions and 19 // limitations under the License. 20 21 // package tcell 22 23 import ( 24 // ic "image/color" 25 "testing" 26 ) 27 28 func TestColorValues(t *testing.T) { 29 values := []struct { 30 color Color 31 hex int32 32 }{ 33 {ColorRed, 0x00FF0000}, 34 {ColorGreen, 0x00008000}, 35 {ColorLime, 0x0000FF00}, 36 {ColorBlue, 0x000000FF}, 37 {ColorBlack, 0x00000000}, 38 {ColorWhite, 0x00FFFFFF}, 39 {ColorSilver, 0x00C0C0C0}, 40 } 41 42 for _, tc := range values { 43 if tc.color.Hex() != tc.hex { 44 t.Errorf("Color: %x != %x", tc.color.Hex(), tc.hex) 45 } 46 } 47 } 48 49 /* 50 func TestColorFitting(t *testing.T) { 51 pal := []Color{} 52 for i := 0; i < 255; i++ { 53 pal = append(pal, PaletteColor(i)) 54 } 55 56 // Exact color fitting on ANSI colors 57 for i := 0; i < 7; i++ { 58 if FindColor(PaletteColor(i), pal[:8]) != PaletteColor(i) { 59 t.Errorf("Color ANSI fit fail at %d", i) 60 } 61 } 62 // Grey is closest to Silver 63 if FindColor(PaletteColor(8), pal[:8]) != PaletteColor(7) { 64 t.Errorf("Grey does not fit to silver") 65 } 66 // Color fitting of upper 8 colors. 67 for i := 9; i < 16; i++ { 68 if FindColor(PaletteColor(i), pal[:8]) != PaletteColor(i%8) { 69 t.Errorf("Color fit fail at %d", i) 70 } 71 } 72 // Imperfect fit 73 if FindColor(ColorOrangeRed, pal[:16]) != ColorRed || 74 FindColor(ColorAliceBlue, pal[:16]) != ColorWhite || 75 FindColor(ColorPink, pal) != Color217 || 76 FindColor(ColorSienna, pal) != Color173 || 77 FindColor(GetColor("#00FD00"), pal) != ColorLime { 78 t.Errorf("Imperfect color fit") 79 } 80 81 } 82 83 */ 84 func TestColorNameLookup(t *testing.T) { 85 values := []struct { 86 name string 87 color Color 88 rgb bool 89 }{ 90 {"#FF0000", ColorRed, true}, 91 {"black", ColorBlack, false}, 92 {"orange", ColorOrange, false}, 93 {"door", ColorDefault, false}, 94 } 95 for _, v := range values { 96 c := GetColor(v.name) 97 if c.Hex() != v.color.Hex() { 98 t.Errorf("Wrong color for %v: %v", v.name, c.Hex()) 99 } 100 if v.rgb { 101 if c&ColorIsRGB == 0 { 102 t.Errorf("Color should have RGB") 103 } 104 } else { 105 if c&ColorIsRGB != 0 { 106 t.Errorf("Named color should not be RGB") 107 } 108 } 109 110 if c.TrueColor().Hex() != v.color.Hex() { 111 t.Errorf("TrueColor did not match") 112 } 113 } 114 } 115 116 func TestColorRGB(t *testing.T) { 117 r, g, b := GetColor("#112233").RGB() 118 if r != 0x11 || g != 0x22 || b != 0x33 { 119 t.Errorf("RGB wrong (%x, %x, %x)", r, g, b) 120 } 121 } 122 123 /* 124 func TestFromImageColor(t *testing.T) { 125 red := ic.RGBA{0xFF, 0x00, 0x00, 0x00} 126 white := ic.Gray{0xFF} 127 cyan := ic.CMYK{0xFF, 0x00, 0x00, 0x00} 128 129 if hex := FromImageColor(red).Hex(); hex != 0xFF0000 { 130 t.Errorf("%v is not 0xFF0000", hex) 131 } 132 if hex := FromImageColor(white).Hex(); hex != 0xFFFFFF { 133 t.Errorf("%v is not 0xFFFFFF", hex) 134 } 135 if hex := FromImageColor(cyan).Hex(); hex != 0x00FFFF { 136 t.Errorf("%v is not 0x00FFFF", hex) 137 } 138 } 139 */ 140 func TestColorString(t *testing.T) { 141 tc := []struct { 142 name string 143 color Color 144 want string 145 isRGB bool 146 }{ 147 {"RGB color string", NewRGBColor(0x35, 0x33, 0x45), "rgb:3535/3333/4545", true}, 148 {"palette color string", PaletteColor(2), "rgb:0000/8080/0000", false}, 149 {"invalid color string", Color(2), "", false}, 150 {"outof range palette color string", Color(379 | ColorValid), "", false}, // any number >378 is undefined color index 151 {"#345678 color string", GetColor("#345678"), "rgb:3434/5656/7878", true}, 152 } 153 154 for _, v := range tc { 155 got := v.color.String() 156 if v.want != got { 157 t.Errorf("%s: expect %s, got %s\n", v.name, v.want, got) 158 } 159 if v.color.IsRGB() != v.isRGB { 160 t.Errorf("%s: expect %t, got %t\n", v.name, v.isRGB, v.color.IsRGB()) 161 } 162 } 163 164 // if Color(379|ColorValid).Hex() != -1 { 165 // t.Errorf("Color(x).Hex() expect return -1, got %d\n ", Color(379|ColorValid).Hex()) 166 // } 167 } 168 169 func TestColorIndex(t *testing.T) { 170 tc := []struct { 171 name string 172 color Color 173 index int 174 }{ 175 {"ANSI 256 color index", Color100, 100}, 176 {"ANSI 8 color index", ColorBlack, 0}, 177 {"ANSI 16 color index", ColorRed, 9}, 178 {"default color", ColorDefault, -1}, // ColorDefault has no index 179 {"RGB color", GetColor("#818181"), -1}, // RGB color has no index 180 } 181 182 for _, v := range tc { 183 got := v.color.Index() 184 if v.index != got { 185 t.Errorf("%s: expect %d, got %d\n", v.name, v.index, v.color.Index()) 186 } 187 } 188 } 189 190 func TestColorName(t *testing.T) { 191 tc := []struct { 192 name string 193 color Color 194 want string 195 }{ 196 {"Balck ", ColorBlack, "black"}, 197 {"Slate grey ", ColorSlateGray, "slategrey"}, 198 {"Slate grey ", ColorSlateGray, "slategray"}, 199 {"Indigo ", ColorIndigo, "indigo"}, 200 {"Absense color", Color108, ""}, 201 } 202 203 for _, v := range tc { 204 names := v.color.Name() 205 // t.Logf("TC:%q %s\n", v.name, names) 206 found := false 207 for _, name := range names { 208 if name == v.want { 209 found = true 210 } 211 } 212 if v.want == "" && names == nil { 213 continue 214 } 215 if !found { 216 t.Errorf("%s: expect color name=%s, got nothing.\n", v.name, v.want) 217 } 218 219 } 220 }