Skip to content

Commit

Permalink
[refactor] use preset color configs instead of on-line generated (#12)
Browse files Browse the repository at this point in the history
* Draft for preset

* For hex names

* Use predefined instead

* Verify color name as vice verse

* Rename

* Update comments
  • Loading branch information
hyorigo committed Oct 27, 2023
1 parent b14b43a commit 62021d3
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 127 deletions.
126 changes: 0 additions & 126 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,11 @@ package blink1

import (
"image/color"
"sort"
"strings"
"sync"
)

var (
// ColorApricot is a predefined color, it's a light orange color similar to the color of an apricot fruit, having the RGB values #FBCEB1
ColorApricot = color.RGBA{R: 0xFB, G: 0xCE, B: 0xB1, A: 0xFF}
// ColorBeige is a predefined color, which is a very pale yellowish-brown color, having the RGB values #F5F5DC
ColorBeige = color.RGBA{R: 0xF5, G: 0xF5, B: 0xDC, A: 0xFF}
// ColorBlack is a predefined color, which absorbs all light in the visible wavelengths, having the RGB values #000000
ColorBlack = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
// ColorBlue is a predefined color, which is one of the primary additive colors, having the RGB values #0000FF
ColorBlue = color.RGBA{R: 0x00, G: 0x00, B: 0xFF, A: 0xFF}
// ColorBronze is a predefined color, it resembles the color of the metal bronze, having the RGB values #CD7F32
ColorBronze = color.RGBA{R: 0xCD, G: 0x7F, B: 0x32, A: 0xFF}
// ColorBrown is a predefined color, which is a composite color produced by mixing red, yellow, and black, having the RGB values #A52A2A
ColorBrown = color.RGBA{R: 0xA5, G: 0x2A, B: 0x2A, A: 0xFF}
// ColorCyan is a predefined color (a.k.a. Aqua), which is a greenish-blue color, having the RGB values #00FFFF
ColorCyan = color.RGBA{R: 0x00, G: 0xFF, B: 0xFF, A: 0xFF}
// ColorGold is a predefined color, which resembles the metal gold, having the RGB values #FFD700
ColorGold = color.RGBA{R: 0xFF, G: 0xD7, B: 0x00, A: 0xFF}
// ColorGray is a predefined color (a.k.a. Grey), which is an intermediate color between black and white, having the RGB values #808080
ColorGray = color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xFF}
// ColorGreen is a predefined color, which is one of the primary additive colors, having the RGB values #00FF00
ColorGreen = color.RGBA{R: 0x00, G: 0xFF, B: 0x00, A: 0xFF}
// ColorIndigo is a predefined color, which is a mix of blue and violet, having the RGB values #4B0082
ColorIndigo = color.RGBA{R: 0x4B, G: 0x00, B: 0x82, A: 0xFF}
// ColorLavender is a predefined color, it's a light purple color similar to lavender flowers, having the RGB values #E6E6FA
ColorLavender = color.RGBA{R: 0xE6, G: 0xE6, B: 0xFA, A: 0xFF}
// ColorLime is a predefined color, which is a fluorescent green, having the RGB values #00FF00
ColorLime = color.RGBA{R: 0x00, G: 0x80, B: 0x00, A: 0xFF}
// ColorMagenta is a predefined color (a.k.a. Fuchsia), which is a mix of red and blue, having the RGB values #FF00FF
ColorMagenta = color.RGBA{R: 0xFF, G: 0x00, B: 0xFF, A: 0xFF}
// ColorMaroon is a predefined color, which is a dark brownish red color, having the RGB values #800000
ColorMaroon = color.RGBA{R: 0x80, G: 0x00, B: 0x00, A: 0xFF}
// ColorMint is a predefined color, which is a pale greenish-blue color, having the RGB values #16982B
ColorMint = color.RGBA{R: 0x16, G: 0x98, B: 0x2B, A: 0xFF}
// ColorNavy is a predefined color, which is a very dark shade of blue, having the RGB values #000080
ColorNavy = color.RGBA{R: 0x00, G: 0x00, B: 0x80, A: 0xFF}
// ColorOlive is a predefined color, which resembles unripe green olives, having the RGB values #808000
ColorOlive = color.RGBA{R: 0x80, G: 0x80, B: 0x00, A: 0xFF}
// ColorOrange is a predefined color, which is between red and yellow, having the RGB values #FFA500
ColorOrange = color.RGBA{R: 0xFF, G: 0xA5, B: 0x00, A: 0xFF}
// ColorPeach is a predefined color, it's similar to the color of a peach fruit, having the RGB values #FFE5B4
ColorPeach = color.RGBA{R: 0xFF, G: 0xE5, B: 0xB4, A: 0xFF}
// ColorPink is a predefined color, which is a pale red color, having the RGB values #FFC0CB
ColorPink = color.RGBA{R: 0xFF, G: 0xC0, B: 0xCB, A: 0xFF}
// ColorPlum is a predefined color, it's a dark purple color similar to the color of a plum fruit, having the RGB values #8E4585
ColorPlum = color.RGBA{R: 0x8E, G: 0x45, B: 0x85, A: 0xFF}
// ColorPurple is a predefined color, which is a mix of red and blue, having the RGB values #800080
ColorPurple = color.RGBA{R: 0x80, G: 0x00, B: 0x80, A: 0xFF}
// ColorRed is a predefined color, which is one of the primary additive colors, having the RGB values #FF0000
ColorRed = color.RGBA{R: 0xFF, G: 0x00, B: 0x00, A: 0xFF}
// ColorScarlet is a predefined color, which is a bright red color with a slightly orange tinge, having the RGB values #FF2400
ColorScarlet = color.RGBA{R: 0xFF, G: 0x24, B: 0x00, A: 0xFF}
// ColorSilver is a predefined color, which resembles gray metallic silver, having the RGB values #C0C0C0
ColorSilver = color.RGBA{R: 0xC0, G: 0xC0, B: 0xC0, A: 0xFF}
// ColorTeal is a predefined color, which is a dark cyan color, having the RGB values #008080
ColorTeal = color.RGBA{R: 0x00, G: 0x80, B: 0x80, A: 0xFF}
// ColorViolet is a predefined color, which is a mix of blue and red, having the RGB values #8000FF
ColorViolet = color.RGBA{R: 0x80, G: 0x00, B: 0xFF, A: 0xFF}
// ColorWhite is a predefined color, which reflects all visible wavelengths of light, having the RGB values #FFFFFF
ColorWhite = color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
// ColorYellow is a predefined color, which is one of the primary subtractive colors, having the RGB values #FFFF00
ColorYellow = color.RGBA{R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF}

// RainbowColors is a predefined color palette, which contains the 7 colors of the rainbow.
RainbowColors = []color.Color{ColorRed, ColorOrange, ColorYellow, ColorGreen, ColorCyan, ColorBlue, ColorViolet}
)

// presetColorMap is a map of all supported preset color names to color values.
var presetColorMap = map[string]color.Color{
"apricot": ColorApricot,
"aqua": ColorCyan,
"beige": ColorBeige,
"black": ColorBlack,
"blue": ColorBlue,
"bronze": ColorBronze,
"brown": ColorBrown,
"cyan": ColorCyan,
"fuchsia": ColorMagenta,
"gold": ColorGold,
"gray": ColorGray,
"green": ColorGreen,
"grey": ColorGray,
"indigo": ColorIndigo,
"lavender": ColorLavender,
"lime": ColorLime,
"magenta": ColorMagenta,
"maroon": ColorMaroon,
"mint": ColorMint,
"navy": ColorNavy,
"olive": ColorOlive,
"orange": ColorOrange,
"peach": ColorPeach,
"pink": ColorPink,
"plum": ColorPlum,
"purple": ColorPurple,
"red": ColorRed,
"scarlet": ColorScarlet,
"silver": ColorSilver,
"teal": ColorTeal,
"violet": ColorViolet,
"white": ColorWhite,
"yellow": ColorYellow,
}

var (
nameOnce sync.Once
colorNames []string
hexNameMap map[string]string
emptyStr string
)

func initNames() {
colorNames = make([]string, 0, len(presetColorMap))
hexNameMap = make(map[string]string, len(presetColorMap))
for name, col := range presetColorMap {
colorNames = append(colorNames, name)
hexNameMap[convColorToHex(col)] = name
}
sort.Strings(colorNames)
}

// GetColorNames returns the color names from the preset color map.
func GetColorNames() []string {
// init name maps
nameOnce.Do(initNames)
// copy name slice
cls := make([]string, len(colorNames))
copy(cls, colorNames)
Expand All @@ -149,8 +25,6 @@ func GetColorByName(name string) (cl color.Color, found bool) {
// If the color is found, it returns the name and true.
// If the color is not found, it returns the hex string and false.
func GetNameByColor(cl color.Color) (name string, found bool) {
// init name maps
nameOnce.Do(initNames)
// check if color is in map
if name, ok := hexNameMap[convColorToHex(cl)]; ok {
return name, true
Expand Down
30 changes: 30 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,36 @@ func TestGetColorNames(t *testing.T) {
if ns3[0] != "apricot" {
t.Errorf("GetColorNames() should not be mutable, got %v", ns3[0])
}

except := []string{"aqua", "fuchsia", "grey"}
for _, n := range ns3 {
// skip excepted names
ignored := false
for _, e := range except {
if n == e {
ignored = true
break
}
}
if ignored {
continue
}

// get color by name
c, ok := b1.GetColorByName(n)
if !ok {
t.Errorf("GetColorByName(%q) should return true", n)
}
// get name by color as reverse
n2, ok2 := b1.GetNameByColor(c)
if !ok2 {
t.Errorf("GetNameByColor(%v) should return true", c)
}
// compare names
if n != n2 {
t.Errorf("GetNameByColor(%v) should return %q, got %q", c, n, n2)
}
}
}

func TestGetColorByName(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
// Currently there is no noticeable performance gain from calling this function before using other APIs.
func Preload() {
regexOnce.Do(initRegex)
nameOnce.Do(initNames)
}

// IsRunningOnSupportedOS returns true if the current OS is supported by underlying HID library.
Expand Down
2 changes: 2 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var (
fadeMsecRegexPats = make(map[int]*regexp.Regexp)
ledIdxRegexPats = make(map[int]*regexp.Regexp)

emptyStr string

errNoTitleMatch = errors.New("b1: no title match")
errNoRepeatMatch = errors.New("b1: no repeat times match")
errNoColorMatch = errors.New("b1: no color match")
Expand Down
146 changes: 146 additions & 0 deletions preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package blink1

import "image/color"

var (
// ColorApricot is a predefined color, it's a light orange color similar to the color of an apricot fruit, having the RGB values #FBCEB1
ColorApricot = color.RGBA{R: 0xFB, G: 0xCE, B: 0xB1, A: 0xFF}
// ColorBeige is a predefined color, which is a very pale yellowish-brown color, having the RGB values #F5F5DC
ColorBeige = color.RGBA{R: 0xF5, G: 0xF5, B: 0xDC, A: 0xFF}
// ColorBlack is a predefined color, which absorbs all light in the visible wavelengths, having the RGB values #000000
ColorBlack = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
// ColorBlue is a predefined color, which is one of the primary additive colors, having the RGB values #0000FF
ColorBlue = color.RGBA{R: 0x00, G: 0x00, B: 0xFF, A: 0xFF}
// ColorBronze is a predefined color, it resembles the color of the metal bronze, having the RGB values #CD7F32
ColorBronze = color.RGBA{R: 0xCD, G: 0x7F, B: 0x32, A: 0xFF}
// ColorBrown is a predefined color, which is a composite color produced by mixing red, yellow, and black, having the RGB values #A52A2A
ColorBrown = color.RGBA{R: 0xA5, G: 0x2A, B: 0x2A, A: 0xFF}
// ColorCyan is a predefined color (a.k.a. Aqua), which is a greenish-blue color, having the RGB values #00FFFF
ColorCyan = color.RGBA{R: 0x00, G: 0xFF, B: 0xFF, A: 0xFF}
// ColorGold is a predefined color, which resembles the metal gold, having the RGB values #FFD700
ColorGold = color.RGBA{R: 0xFF, G: 0xD7, B: 0x00, A: 0xFF}
// ColorGray is a predefined color (a.k.a. Grey), which is an intermediate color between black and white, having the RGB values #808080
ColorGray = color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0xFF}
// ColorGreen is a predefined color, which is one of the primary additive colors, having the RGB values #00FF00
ColorGreen = color.RGBA{R: 0x00, G: 0xFF, B: 0x00, A: 0xFF}
// ColorIndigo is a predefined color, which is a mix of blue and violet, having the RGB values #4B0082
ColorIndigo = color.RGBA{R: 0x4B, G: 0x00, B: 0x82, A: 0xFF}
// ColorLavender is a predefined color, it's a light purple color similar to lavender flowers, having the RGB values #E6E6FA
ColorLavender = color.RGBA{R: 0xE6, G: 0xE6, B: 0xFA, A: 0xFF}
// ColorLime is a predefined color, which is a fluorescent green, having the RGB values #00FF00
ColorLime = color.RGBA{R: 0x00, G: 0x80, B: 0x00, A: 0xFF}
// ColorMagenta is a predefined color (a.k.a. Fuchsia), which is a mix of red and blue, having the RGB values #FF00FF
ColorMagenta = color.RGBA{R: 0xFF, G: 0x00, B: 0xFF, A: 0xFF}
// ColorMaroon is a predefined color, which is a dark brownish red color, having the RGB values #800000
ColorMaroon = color.RGBA{R: 0x80, G: 0x00, B: 0x00, A: 0xFF}
// ColorMint is a predefined color, which is a pale greenish-blue color, having the RGB values #16982B
ColorMint = color.RGBA{R: 0x16, G: 0x98, B: 0x2B, A: 0xFF}
// ColorNavy is a predefined color, which is a very dark shade of blue, having the RGB values #000080
ColorNavy = color.RGBA{R: 0x00, G: 0x00, B: 0x80, A: 0xFF}
// ColorOlive is a predefined color, which resembles unripe green olives, having the RGB values #808000
ColorOlive = color.RGBA{R: 0x80, G: 0x80, B: 0x00, A: 0xFF}
// ColorOrange is a predefined color, which is between red and yellow, having the RGB values #FFA500
ColorOrange = color.RGBA{R: 0xFF, G: 0xA5, B: 0x00, A: 0xFF}
// ColorPeach is a predefined color, it's similar to the color of a peach fruit, having the RGB values #FFE5B4
ColorPeach = color.RGBA{R: 0xFF, G: 0xE5, B: 0xB4, A: 0xFF}
// ColorPink is a predefined color, which is a pale red color, having the RGB values #FFC0CB
ColorPink = color.RGBA{R: 0xFF, G: 0xC0, B: 0xCB, A: 0xFF}
// ColorPlum is a predefined color, it's a dark purple color similar to the color of a plum fruit, having the RGB values #8E4585
ColorPlum = color.RGBA{R: 0x8E, G: 0x45, B: 0x85, A: 0xFF}
// ColorPurple is a predefined color, which is a mix of red and blue, having the RGB values #800080
ColorPurple = color.RGBA{R: 0x80, G: 0x00, B: 0x80, A: 0xFF}
// ColorRed is a predefined color, which is one of the primary additive colors, having the RGB values #FF0000
ColorRed = color.RGBA{R: 0xFF, G: 0x00, B: 0x00, A: 0xFF}
// ColorScarlet is a predefined color, which is a bright red color with a slightly orange tinge, having the RGB values #FF2400
ColorScarlet = color.RGBA{R: 0xFF, G: 0x24, B: 0x00, A: 0xFF}
// ColorSilver is a predefined color, which resembles gray metallic silver, having the RGB values #C0C0C0
ColorSilver = color.RGBA{R: 0xC0, G: 0xC0, B: 0xC0, A: 0xFF}
// ColorTeal is a predefined color, which is a dark cyan color, having the RGB values #008080
ColorTeal = color.RGBA{R: 0x00, G: 0x80, B: 0x80, A: 0xFF}
// ColorViolet is a predefined color, which is a mix of blue and red, having the RGB values #8000FF
ColorViolet = color.RGBA{R: 0x80, G: 0x00, B: 0xFF, A: 0xFF}
// ColorWhite is a predefined color, which reflects all visible wavelengths of light, having the RGB values #FFFFFF
ColorWhite = color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
// ColorYellow is a predefined color, which is one of the primary subtractive colors, having the RGB values #FFFF00
ColorYellow = color.RGBA{R: 0xFF, G: 0xFF, B: 0x00, A: 0xFF}

// RainbowColors is a predefined color palette, which contains the 7 colors of the rainbow.
RainbowColors = []color.Color{ColorRed, ColorOrange, ColorYellow, ColorGreen, ColorCyan, ColorBlue, ColorViolet}
)

// presetColorMap is a map of all supported preset color names to color values.
// Don't forget to update this map when adding a new color as Color*.
var presetColorMap = map[string]color.Color{
"apricot": ColorApricot,
"aqua": ColorCyan,
"beige": ColorBeige,
"black": ColorBlack,
"blue": ColorBlue,
"bronze": ColorBronze,
"brown": ColorBrown,
"cyan": ColorCyan,
"fuchsia": ColorMagenta,
"gold": ColorGold,
"gray": ColorGray,
"green": ColorGreen,
"grey": ColorGray,
"indigo": ColorIndigo,
"lavender": ColorLavender,
"lime": ColorLime,
"magenta": ColorMagenta,
"maroon": ColorMaroon,
"mint": ColorMint,
"navy": ColorNavy,
"olive": ColorOlive,
"orange": ColorOrange,
"peach": ColorPeach,
"pink": ColorPink,
"plum": ColorPlum,
"purple": ColorPurple,
"red": ColorRed,
"scarlet": ColorScarlet,
"silver": ColorSilver,
"teal": ColorTeal,
"violet": ColorViolet,
"white": ColorWhite,
"yellow": ColorYellow,
}

// hexNameMap is a map of all supported preset color hex strings to color names (formal name, not alias). It should be generated from presetColorMap.
var hexNameMap = map[string]string{
"#000000": "black",
"#000080": "navy",
"#0000FF": "blue",
"#008000": "lime",
"#008080": "teal",
"#00FF00": "green",
"#00FFFF": "cyan",
"#16982B": "mint",
"#4B0082": "indigo",
"#800000": "maroon",
"#800080": "purple",
"#8000FF": "violet",
"#808000": "olive",
"#808080": "gray",
"#8E4585": "plum",
"#A52A2A": "brown",
"#C0C0C0": "silver",
"#CD7F32": "bronze",
"#E6E6FA": "lavender",
"#F5F5DC": "beige",
"#FBCEB1": "apricot",
"#FF0000": "red",
"#FF00FF": "magenta",
"#FF2400": "scarlet",
"#FFA500": "orange",
"#FFC0CB": "pink",
"#FFD700": "gold",
"#FFE5B4": "peach",
"#FFFF00": "yellow",
"#FFFFFF": "white",
}

// colorNames is a slice of all supported preset color names. It should be generated from presetColorMap as well.
var colorNames = []string{
"apricot", "aqua", "beige", "black", "blue", "bronze", "brown", "cyan", "fuchsia", "gold", "gray", "green", "grey", "indigo", "lavender", "lime", "magenta", "maroon", "mint", "navy", "olive", "orange", "peach", "pink", "plum", "purple", "red", "scarlet", "silver", "teal", "violet", "white", "yellow",
}

0 comments on commit 62021d3

Please sign in to comment.