github.com/go-graphite/carbonapi@v0.17.0/expr/functions/cairo/png/graphutil.go (about) 1 package png 2 3 import ( 4 "image/color" 5 "strconv" 6 "strings" 7 ) 8 9 func getBool(s string, def bool) bool { 10 if s == "" { 11 return def 12 } 13 14 switch s { 15 case "True", "true", "1": 16 return true 17 case "False", "false", "0": 18 return false 19 } 20 21 return def 22 } 23 24 func getString(s, def string) string { 25 if s == "" { 26 return def 27 } 28 29 return s 30 } 31 32 func getFloat64(s string, def float64) float64 { 33 if s == "" { 34 return def 35 } 36 37 n, err := strconv.ParseFloat(s, 64) 38 if err != nil { 39 return def 40 } 41 42 return n 43 } 44 45 func getInt(s string, def int) int { 46 if s == "" { 47 return def 48 } 49 n, err := strconv.ParseInt(s, 10, 32) 50 if err != nil { 51 return def 52 } 53 return int(n) 54 } 55 56 // For some reason, deepsource thinks this function is not used, even though it's actually used whenever Cairo is enabled. 57 // skipcq: SCC-U1000 58 // lint:ignore U1000 - false-positive 59 func string2RGBA(clr string) color.RGBA { 60 if c, ok := colors[clr]; ok { 61 return c 62 } 63 c, err := hexToRGBA(clr) 64 if err != nil { 65 return color.RGBA{0, 0, 0, 255} 66 } 67 return *c 68 } 69 70 // https://code.google.com/p/sadbox/source/browse/color/hex.go 71 // hexToRGBA converts an Hex string to a RGB triple. 72 func hexToRGBA(h string) (*color.RGBA, error) { 73 var r, g, b uint8 74 if len(h) > 0 && h[0] == '#' { 75 h = h[1:] 76 } 77 78 if len(h) == 3 { 79 h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:] 80 } 81 82 alpha := byte(255) 83 84 if len(h) == 6 { 85 if rgb, err := strconv.ParseUint(h, 16, 32); err == nil { 86 r = uint8(rgb >> 16) 87 g = uint8(rgb >> 8) 88 b = uint8(rgb) 89 } else { 90 return nil, err 91 } 92 } 93 94 if len(h) == 8 { 95 if rgb, err := strconv.ParseUint(h, 16, 32); err == nil { 96 r = uint8(rgb >> 24) 97 g = uint8(rgb >> 16) 98 b = uint8(rgb >> 8) 99 alpha = uint8(rgb) 100 } else { 101 return nil, err 102 } 103 } 104 105 return &color.RGBA{r, g, b, alpha}, nil 106 } 107 108 var colors = map[string]color.RGBA{ 109 // Graphite default colors 110 "black": {0x00, 0x00, 0x00, 0xff}, 111 "white": {0xff, 0xff, 0xff, 0xff}, 112 // blue is intentionally specified in that way (not really blue ;) ) to behave like graphite-web 1.1 113 "blue": {0x64, 0x64, 0xff, 0xff}, 114 // green and darkgreen intentionally swapped. You can redefined that in config 115 "green": {0x00, 0xc8, 0x00, 0xff}, 116 // red and darkred intentionally swapped. You can redefined that in config 117 "red": {0xc8, 0x00, 0x32, 0xff}, 118 "yellow": {0xff, 0xff, 0x00, 0xff}, 119 "orange": {0xff, 0xa5, 0x00, 0xff}, 120 "purple": {0xc8, 0x64, 0xff, 0xff}, 121 "brown": {0x96, 0x64, 0x32, 0xff}, 122 "cyan": {0x00, 0xff, 0xff, 0xff}, 123 "aqua": {0x00, 0x96, 0x96, 0xff}, 124 "gray": {0xaf, 0xaf, 0xaf, 0xff}, 125 "grey": {0xaf, 0xaf, 0xaf, 0xff}, 126 "magenta": {0xff, 0x00, 0xff, 0xff}, 127 "pink": {0xff, 0x64, 0x64, 0xff}, 128 "gold": {0xc8, 0xc8, 0x00, 0xff}, 129 "rose": {0xc8, 0x96, 0xc8, 0xff}, 130 // darkblue is intentionally specified in that way to behave like graphite-web 1.1 131 "darkblue": {0x00, 0x00, 0xff, 0xff}, 132 // green and darkgreen intentionally swapped. You can redefined that in config 133 "darkgreen": {0x00, 0xff, 0x00, 0xff}, 134 // red and darkred intentionally swapped. You can redefined that in config 135 "darkred": {0xff, 0x00, 0x00, 0xff}, 136 "darkgray": {0x6f, 0x6f, 0x6f, 0xff}, 137 "darkgrey": {0x6f, 0x6f, 0x6f, 0xff}, 138 139 // Custom colors 140 "navy": {0x00, 0x00, 0x80, 0xff}, 141 "mediumblue": {0x00, 0x00, 0xcd, 0xff}, 142 "teal": {0x00, 0x80, 0x80, 0xff}, 143 "darkcyan": {0x00, 0x8b, 0x8b, 0xff}, 144 "deepskyblue": {0x00, 0xbf, 0xff, 0xff}, 145 "darkturquoise": {0x00, 0xce, 0xd1, 0xff}, 146 "mediumspringgreen": {0x00, 0xfa, 0x9a, 0xff}, 147 "lime": {0x00, 0xff, 0x00, 0xff}, 148 "springgreen": {0x00, 0xff, 0x7f, 0xff}, 149 "midnightblue": {0x19, 0x19, 0x70, 0xff}, 150 "dodgerblue": {0x1e, 0x90, 0xff, 0xff}, 151 "lightseagreen": {0x20, 0xb2, 0xaa, 0xff}, 152 "forestgreen": {0x22, 0x8b, 0x22, 0xff}, 153 "seagreen": {0x2e, 0x8b, 0x57, 0xff}, 154 "darkslategray": {0x2f, 0x4f, 0x4f, 0xff}, 155 "limegreen": {0x32, 0xcd, 0x32, 0xff}, 156 "mediumseagreen": {0x3c, 0xb3, 0x71, 0xff}, 157 "turquoise": {0x40, 0xe0, 0xd0, 0xff}, 158 "royalblue": {0x41, 0x69, 0xe1, 0xff}, 159 "steelblue": {0x46, 0x82, 0xb4, 0xff}, 160 "darkslateblue": {0x48, 0x3d, 0x8b, 0xff}, 161 "mediumturquoise": {0x48, 0xd1, 0xcc, 0xff}, 162 "indigo": {0x4b, 0x00, 0x82, 0xff}, 163 "darkolivegreen": {0x55, 0x6b, 0x2f, 0xff}, 164 "cadetblue": {0x5f, 0x9e, 0xa0, 0xff}, 165 "cornflowerblue": {0x64, 0x95, 0xed, 0xff}, 166 "mediumaquamarine": {0x66, 0xcd, 0xaa, 0xff}, 167 "dimgray": {0x69, 0x69, 0x69, 0xff}, 168 "slateblue": {0x6a, 0x5a, 0xcd, 0xff}, 169 "olivedrab": {0x6b, 0x8e, 0x23, 0xff}, 170 "slategray": {0x70, 0x80, 0x90, 0xff}, 171 "lightslategray": {0x77, 0x88, 0x99, 0xff}, 172 "mediumslateblue": {0x7b, 0x68, 0xee, 0xff}, 173 "lawngreen": {0x7c, 0xfc, 0x00, 0xff}, 174 "chartreuse": {0x7f, 0xff, 0x00, 0xff}, 175 "aquamarine": {0x7f, 0xff, 0xd4, 0xff}, 176 "lavender": {0xe6, 0xe6, 0xfa, 0xff}, 177 "darksalmon": {0xe9, 0x96, 0x7a, 0xff}, 178 "violet": {0xee, 0x82, 0xee, 0xff}, 179 "palegoldenrod": {0xee, 0xe8, 0xaa, 0xff}, 180 "lightcoral": {0xf0, 0x80, 0x80, 0xff}, 181 "khaki": {0xf0, 0xe6, 0x8c, 0xff}, 182 "aliceblue": {0xf0, 0xf8, 0xff, 0xff}, 183 "honeydew": {0xf0, 0xff, 0xf0, 0xff}, 184 "azure": {0xf0, 0xff, 0xff, 0xff}, 185 "sandybrown": {0xf4, 0xa4, 0x60, 0xff}, 186 "wheat": {0xf5, 0xde, 0xb3, 0xff}, 187 "beige": {0xf5, 0xf5, 0xdc, 0xff}, 188 "whitesmoke": {0xf5, 0xf5, 0xf5, 0xff}, 189 "mintcream": {0xf5, 0xff, 0xfa, 0xff}, 190 "ghostwhite": {0xf8, 0xf8, 0xff, 0xff}, 191 "salmon": {0xfa, 0x80, 0x72, 0xff}, 192 "antiquewhite": {0xfa, 0xeb, 0xd7, 0xff}, 193 "linen": {0xfa, 0xf0, 0xe6, 0xff}, 194 "lightgoldenrodyellow": {0xfa, 0xfa, 0xd2, 0xff}, 195 "oldlace": {0xfd, 0xf5, 0xe6, 0xff}, 196 "fuchsia": {0xff, 0x00, 0xff, 0xff}, 197 "deeppink": {0xff, 0x14, 0x93, 0xff}, 198 "orangered": {0xff, 0x45, 0x00, 0xff}, 199 "tomato": {0xff, 0x63, 0x47, 0xff}, 200 "hotpink": {0xff, 0x69, 0xb4, 0xff}, 201 "coral": {0xff, 0x7f, 0x50, 0xff}, 202 "darkorange": {0xff, 0x8c, 0x00, 0xff}, 203 "lightsalmon": {0xff, 0xa0, 0x7a, 0xff}, 204 "lightpink": {0xff, 0xb6, 0xc1, 0xff}, 205 "peachpuff": {0xff, 0xda, 0xb9, 0xff}, 206 "navajowhite": {0xff, 0xde, 0xad, 0xff}, 207 "moccasin": {0xff, 0xe4, 0xb5, 0xff}, 208 "bisque": {0xff, 0xe4, 0xc4, 0xff}, 209 "mistyrose": {0xff, 0xe4, 0xe1, 0xff}, 210 "blanchedalmond": {0xff, 0xeb, 0xcd, 0xff}, 211 "papayawhip": {0xff, 0xef, 0xd5, 0xff}, 212 "lavenderblush": {0xff, 0xf0, 0xf5, 0xff}, 213 "seashell": {0xff, 0xf5, 0xee, 0xff}, 214 "cornsilk": {0xff, 0xf8, 0xdc, 0xff}, 215 "lemonchiffon": {0xff, 0xfa, 0xcd, 0xff}, 216 "floralwhite": {0xff, 0xfa, 0xf0, 0xff}, 217 "snow": {0xff, 0xfa, 0xfa, 0xff}, 218 "lightyellow": {0xff, 0xff, 0xe0, 0xff}, 219 "ivory": {0xff, 0xff, 0xf0, 0xff}, 220 "graphiteblue": {0x64, 0x64, 0xff, 0xff}, 221 } 222 223 func SetColor(name, rgba string) error { 224 color, err := hexToRGBA(rgba) 225 if err != nil { 226 return err 227 } 228 229 name = strings.ToLower(name) 230 colors[name] = *color 231 return nil 232 }