Skip to content

Commit e48d1e5

Browse files
committed
enhance: support use color name create some common rgb color
1 parent 19b21c9 commit e48d1e5

File tree

4 files changed

+290
-6
lines changed

4 files changed

+290
-6
lines changed

color_rgb.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,8 @@ func Rgb(r, g, b uint8, isBg ...bool) RGBColor { return RGB(r, g, b, isBg...) }
9797
// Bit24 alias of the RGB()
9898
func Bit24(r, g, b uint8, isBg ...bool) RGBColor { return RGB(r, g, b, isBg...) }
9999

100-
// RGBFromSlice quick RGBColor from slice
101-
func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor {
102-
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
103-
}
104-
105100
// HEX create RGB color from a HEX color string.
101+
//
106102
// Usage:
107103
// c := HEX("ccc") // rgb: [204 204 204]
108104
// c := HEX("aabbcc") // rgb: [170 187 204]
@@ -121,11 +117,40 @@ func HEX(hex string, isBg ...bool) RGBColor {
121117
// Hex alias of the HEX()
122118
func Hex(hex string, isBg ...bool) RGBColor { return HEX(hex, isBg...) }
123119

120+
// HSL create RGB color from a hsl value.
121+
func HSL(h, s, l float32, isBg ...bool) RGBColor {
122+
if rgb := HslToRgb(h, s, l); len(rgb) > 0 {
123+
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
124+
}
125+
126+
// mark is empty
127+
return emptyRGBColor
128+
}
129+
130+
// Hsl alias of the HSL()
131+
func Hsl(h, s, l float32, isBg ...bool) RGBColor { return HSL(h, s, l, isBg...) }
132+
133+
// RGBFromSlice quick RGBColor from slice
134+
func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor {
135+
return RGB(rgb[0], rgb[1], rgb[2], isBg...)
136+
}
137+
124138
// RGBFromString create RGB color from a string.
139+
// support use color name in the {namedRgbColor}
140+
//
125141
// Usage:
126142
// c := RGBFromString("170,187,204")
127143
// c.Print("message")
144+
//
145+
// c := RGBFromString("brown")
146+
// c.Print("message with color brown")
128147
func RGBFromString(rgb string, isBg ...bool) RGBColor {
148+
// use color name in the {namedRgbColor}
149+
if rgbVal, ok := namedRgbColor[rgb]; ok {
150+
rgb = rgbVal
151+
}
152+
153+
// use rgb string.
129154
ss := stringToArr(rgb, ",")
130155
if len(ss) != 3 {
131156
return emptyRGBColor

color_rgb_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func TestRGBFromString(t *testing.T) {
7575
c = RGBFromString("170,187,204", true)
7676
is.Equal("\x1b[48;2;170;187;204mmsg\x1b[0m", c.Sprint("msg"))
7777

78+
c = RGBFromString("brown")
79+
is.Equal("\x1b[38;2;165;42;42mmsg\x1b[0m", c.Sprint("msg"))
80+
7881
c = RGBFromString("170,187,")
7982
is.Equal("msg", c.Sprint("msg"))
8083

@@ -254,3 +257,9 @@ func TestRGBColor_C16(t *testing.T) {
254257
assert.Equal(t, "46", rgb.C16().String())
255258
assert.Equal(t, "46", rgb.Color().String())
256259
}
260+
261+
func TestHSL(t *testing.T) {
262+
hsl := HSL(0.33, 1, 0.5)
263+
264+
hsl.Println("rgb color create by HSL")
265+
}

convert.go

+245-1
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,255 @@ func C256ToRgbV1(val uint8) (rgb []uint8) {
609609
}
610610

611611
/**************************************************************
612-
* hsl color <=> RGB/True color
612+
* HSL color <=> RGB/True color
613613
************************************************************
614+
* h,s,l = Hue, Saturation, Lightness
615+
*
614616
* refers
617+
* http://en.wikipedia.org/wiki/HSL_color_space
618+
* https://www.w3.org/TR/css-color-3/#hsl-color
615619
* https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
616620
* https://github.com/less/less.js/blob/master/packages/less/src/less/functions/color.js
617621
* https://github.com/d3/d3-color/blob/v3.0.1/README.md#hsl
622+
*
623+
* examples:
624+
* color: hsl(0, 100%, 50%) // red
625+
* color: hsl(120, 100%, 50%) // lime
626+
* color: hsl(120, 100%, 25%) // dark green
627+
* color: hsl(120, 100%, 75%) // light green
628+
* color: hsl(120, 75%, 75%) // pastel green, and so on
618629
*/
619630

631+
// HslToRgbByInt Converts an HSL color value to RGB
632+
// Assumes h: 0-360, s: 0-100, l: 0-100
633+
// returns r, g, and b in the set [0, 255].
634+
//
635+
// Usage:
636+
// HslToRgbByInt(120, 100, 50)
637+
// HslToRgbByInt(120, 100, 25)
638+
func HslToRgbByInt(h, s, l int) (rgb []uint8) {
639+
return HslToRgb(float32(h)/360, float32(s)/100, float32(l)/100)
640+
}
641+
642+
// HslToRgb Converts an HSL color value to RGB. Conversion formula
643+
// adapted from http://en.wikipedia.org/wiki/HSL_color_space.
644+
// Assumes h, s, and l are contained in the set [0, 1]
645+
// returns r, g, and b in the set [0, 255].
646+
//
647+
// Usage:
648+
// rgbVals := HslToRgb(0.33, 1, 0.5)
649+
func HslToRgb(h, s, l float32) (rgb []uint8) {
650+
var r, g, b float32
651+
652+
if s == 0 { // achromatic
653+
r, g, b = l, l, l
654+
} else {
655+
var hue2rgb = func(p, q, t float32) float32 {
656+
if t < 0 {
657+
t += 1
658+
}
659+
if t > 1 {
660+
t -= 1
661+
}
662+
if t < 1/6 {
663+
return p + (q-p)*6*t
664+
}
665+
666+
if t < 1/2 {
667+
return q
668+
}
669+
if t < 2/3 {
670+
return p + (q-p)*(2/3-t)*6
671+
}
672+
return p
673+
}
674+
675+
// q = l < 0.5 ? l * (1 + s) : l + s - l*s
676+
var q float32
677+
if l < 0.5 {
678+
q = l * (1 + s)
679+
} else {
680+
q = l + s - l*s
681+
}
682+
683+
var p = 2*l - q
684+
685+
r = hue2rgb(p, q, h+1/3)
686+
g = hue2rgb(p, q, h)
687+
b = hue2rgb(p, q, h-1/3)
688+
}
689+
690+
// return [math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
691+
return []uint8{uint8(r * 255 / 100), uint8(g * 255 / 100), uint8(b * 255 / 100)}
692+
}
693+
694+
/**************************************************************
695+
* HSV color <=> RGB/True color
696+
************************************************************
697+
* h,s,l = Hue, Saturation, Value(Brightness)
698+
*
699+
* refers
700+
* https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
701+
* https://github.com/less/less.js/blob/master/packages/less/src/less/functions/color.js
702+
* https://github.com/d3/d3-color/blob/v3.0.1/README.md#hsl
703+
*/
704+
705+
// HsvToRgb Converts an HSL color value to RGB. Conversion formula
706+
// adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
707+
// Assumes h: 0-360, s: 0-100, l: 0-100
708+
// returns r, g, and b in the set [0, 255].
709+
func HsvToRgb(h, s, v int) (rgb []uint8) {
710+
return
711+
}
712+
713+
// Named rgb colors
714+
// https://www.w3.org/TR/css-color-3/#svg-color
715+
var namedRgbColor = map[string]string{
716+
"aliceblue": "240,248,255", // #F0F8FF
717+
"antiquewhite": "250,235,215", // #FAEBD7
718+
"aqua": "0,255,255", // #00FFFF
719+
"aquamarine": "127,255,212", // #7FFFD4
720+
"azure": "240,255,255", // #F0FFFF
721+
"beige": "245,245,220", // #F5F5DC
722+
"bisque": "255,228,196", // #FFE4C4
723+
"black": "0,0,0", // #000000
724+
"blanchedalmond": "255,235,205", // #FFEBCD
725+
"blue": "0,0,255", // #0000FF
726+
"blueviolet": "138,43,226", // #8A2BE2
727+
"brown": "165,42,42", // #A52A2A
728+
"burlywood": "222,184,135", // #DEB887
729+
"cadetblue": "95,158,160", // #5F9EA0
730+
"chartreuse": "127,255,0", // #7FFF00
731+
"chocolate": "210,105,30", // #D2691E
732+
"coral": "255,127,80", // #FF7F50
733+
"cornflowerblue": "100,149,237", // #6495ED
734+
"cornsilk": "255,248,220", // #FFF8DC
735+
"crimson": "220,20,60", // #DC143C
736+
"cyan": "0,255,255", // #00FFFF
737+
"darkblue": "0,0,139", // #00008B
738+
"darkcyan": "0,139,139", // #008B8B
739+
"darkgoldenrod": "184,134,11", // #B8860B
740+
"darkgray": "169,169,169", // #A9A9A9
741+
"darkgreen": "0,100,0", // #006400
742+
"darkgrey": "169,169,169", // #A9A9A9
743+
"darkkhaki": "189,183,107", // #BDB76B
744+
"darkmagenta": "139,0,139", // #8B008B
745+
"darkolivegreen": "85,107,47", // #556B2F
746+
"darkorange": "255,140,0", // #FF8C00
747+
"darkorchid": "153,50,204", // #9932CC
748+
"darkred": "139,0,0", // #8B0000
749+
"darksalmon": "233,150,122", // #E9967A
750+
"darkseagreen": "143,188,143", // #8FBC8F
751+
"darkslateblue": "72,61,139", // #483D8B
752+
"darkslategray": "47,79,79", // #2F4F4F
753+
"darkslategrey": "47,79,79", // #2F4F4F
754+
"darkturquoise": "0,206,209", // #00CED1
755+
"darkviolet": "148,0,211", // #9400D3
756+
"deeppink": "255,20,147", // #FF1493
757+
"deepskyblue": "0,191,255", // #00BFFF
758+
"dimgray": "105,105,105", // #696969
759+
"dimgrey": "105,105,105", // #696969
760+
"dodgerblue": "30,144,255", // #1E90FF
761+
"firebrick": "178,34,34", // #B22222
762+
"floralwhite": "255,250,240", // #FFFAF0
763+
"forestgreen": "34,139,34", // #228B22
764+
"fuchsia": "255,0,255", // #FF00FF
765+
"gainsboro": "220,220,220", // #DCDCDC
766+
"ghostwhite": "248,248,255", // #F8F8FF
767+
"gold": "255,215,0", // #FFD700
768+
"goldenrod": "218,165,32", // #DAA520
769+
"gray": "128,128,128", // #808080
770+
"green": "0,128,0", // #008000
771+
"greenyellow": "173,255,47", // #ADFF2F
772+
"grey": "128,128,128", // #808080
773+
"honeydew": "240,255,240", // #F0FFF0
774+
"hotpink": "255,105,180", // #FF69B4
775+
"indianred": "205,92,92", // #CD5C5C
776+
"indigo": "75,0,130", // #4B0082
777+
"ivory": "255,255,240", // #FFFFF0
778+
"khaki": "240,230,140", // #F0E68C
779+
"lavender": "230,230,250", // #E6E6FA
780+
"lavenderblush": "255,240,245", // #FFF0F5
781+
"lawngreen": "124,252,0", // #7CFC00
782+
"lemonchiffon": "255,250,205", // #FFFACD
783+
"lightblue": "173,216,230", // #ADD8E6
784+
"lightcoral": "240,128,128", // #F08080
785+
"lightcyan": "224,255,255", // #E0FFFF
786+
"lightgoldenrodyellow": "250,250,210", // #FAFAD2
787+
"lightgray": "211,211,211", // #D3D3D3
788+
"lightgreen": "144,238,144", // #90EE90
789+
"lightgrey": "211,211,211", // #D3D3D3
790+
"lightpink": "255,182,193", // #FFB6C1
791+
"lightsalmon": "255,160,122", // #FFA07A
792+
"lightseagreen": "32,178,170", // #20B2AA
793+
"lightskyblue": "135,206,250", // #87CEFA
794+
"lightslategray": "119,136,153", // #778899
795+
"lightslategrey": "119,136,153", // #778899
796+
"lightsteelblue": "176,196,222", // #B0C4DE
797+
"lightyellow": "255,255,224", // #FFFFE0
798+
"lime": "0,255,0", // #00FF00
799+
"limegreen": "50,205,50", // #32CD32
800+
"linen": "250,240,230", // #FAF0E6
801+
"magenta": "255,0,255", // #FF00FF
802+
"maroon": "128,0,0", // #800000
803+
"mediumaquamarine": "102,205,170", // #66CDAA
804+
"mediumblue": "0,0,205", // #0000CD
805+
"mediumorchid": "186,85,211", // #BA55D3
806+
"mediumpurple": "147,112,219", // #9370DB
807+
"mediumseagreen": "60,179,113", // #3CB371
808+
"mediumslateblue": "123,104,238", // #7B68EE
809+
"mediumspringgreen": "0,250,154", // #00FA9A
810+
"mediumturquoise": "72,209,204", // #48D1CC
811+
"mediumvioletred": "199,21,133", // #C71585
812+
"midnightblue": "25,25,112", // #191970
813+
"mintcream": "245,255,250", // #F5FFFA
814+
"mistyrose": "255,228,225", // #FFE4E1
815+
"moccasin": "255,228,181", // #FFE4B5
816+
"navajowhite": "255,222,173", // #FFDEAD
817+
"navy": "0,0,128", // #000080
818+
"oldlace": "253,245,230", // #FDF5E6
819+
"olive": "128,128,0", // #808000
820+
"olivedrab": "107,142,35", // #6B8E23
821+
"orange": "255,165,0", // #FFA500
822+
"orangered": "255,69,0", // #FF4500
823+
"orchid": "218,112,214", // #DA70D6
824+
"palegoldenrod": "238,232,170", // #EEE8AA
825+
"palegreen": "152,251,152", // #98FB98
826+
"paleturquoise": "175,238,238", // #AFEEEE
827+
"palevioletred": "219,112,147", // #DB7093
828+
"papayawhip": "255,239,213", // #FFEFD5
829+
"peachpuff": "255,218,185", // #FFDAB9
830+
"peru": "205,133,63", // #CD853F
831+
"pink": "255,192,203", // #FFC0CB
832+
"plum": "221,160,221", // #DDA0DD
833+
"powderblue": "176,224,230", // #B0E0E6
834+
"purple": "128,0,128", // #800080
835+
"red": "255,0,0", // #FF0000
836+
"rosybrown": "188,143,143", // #BC8F8F
837+
"royalblue": "65,105,225", // #4169E1
838+
"saddlebrown": "139,69,19", // #8B4513
839+
"salmon": "250,128,114", // #FA8072
840+
"sandybrown": "244,164,96", // #F4A460
841+
"seagreen": "46,139,87", // #2E8B57
842+
"seashell": "255,245,238", // #FFF5EE
843+
"sienna": "160,82,45", // #A0522D
844+
"silver": "192,192,192", // #C0C0C0
845+
"skyblue": "135,206,235", // #87CEEB
846+
"slateblue": "106,90,205", // #6A5ACD
847+
"slategray": "112,128,144", // #708090
848+
"slategrey": "112,128,144", // #708090
849+
"snow": "255,250,250", // #FFFAFA
850+
"springgreen": "0,255,127", // #00FF7F
851+
"steelblue": "70,130,180", // #4682B4
852+
"tan": "210,180,140", // #D2B48C
853+
"teal": "0,128,128", // #008080
854+
"thistle": "216,191,216", // #D8BFD8
855+
"tomato": "255,99,71", // #FF6347
856+
"turquoise": "64,224,208", // #40E0D0
857+
"violet": "238,130,238", // #EE82EE
858+
"wheat": "245,222,179", // #F5DEB3
859+
"white": "255,255,255", // #FFFFFF
860+
"whitesmoke": "245,245,245", // #F5F5F5
861+
"yellow": "255,255,0", // #FFFF00
862+
"yellowgreen": "154,205,50", // #9ACD32
863+
}

utils_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,9 @@ func TestRgb2basic(t *testing.T) {
428428
assert.Equal(t, uint8(41), Rgb2basic(134, 56, 56, true))
429429
assert.Equal(t, uint8(46), Rgb2basic(57, 187, 226, true))
430430
}
431+
432+
func TestHslToRgb(t *testing.T) {
433+
HslToRgbByInt(120, 100, 50)
434+
HslToRgbByInt(120, 100, 25)
435+
HslToRgb(0.33, 1, 0.5)
436+
}

0 commit comments

Comments
 (0)