gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/sgr/color.go (about) 1 package sgr 2 3 import ( 4 "strconv" 5 ) 6 7 type ColorRGB struct { 8 R uint8 9 G uint8 10 B uint8 11 } 12 13 func RGB(r, g, b uint8) ColorRGB { 14 return ColorRGB{R: r, G: g, B: b} 15 } 16 17 func RGB24(rgb uint32) ColorRGB { 18 return ColorRGB{R: byte((rgb >> 16) & 0xff), G: byte((rgb >> 8) & 0xff), B: byte(rgb & 0xff)} 19 } 20 21 type Color256 uint8 22 23 func WrapColor(s string, color Flag) string { 24 return WrapFlag(s, color.Color()) 25 } 26 27 func WrapColor256(s string, color256 Color256) string { 28 return WrapCodes(s, CustomFgColorCode, "5", u8ts[color256]) 29 } 30 31 func WrapColorRGB(s string, rgb ColorRGB) string { 32 return WrapCodes(s, CustomFgColorCode, "2", u8ts[rgb.R], u8ts[rgb.G], u8ts[rgb.B]) 33 } 34 35 func WrapRGB(s string, r, g, b uint8) string { 36 return WrapColorRGB(s, RGB(r, g, b)) 37 } 38 39 func WrapRGB24(s string, rgb uint32) string { 40 return WrapColorRGB(s, RGB24(rgb)) 41 } 42 43 var ( 44 fgNormalColorTable [256]string 45 fgBrightColorTable [256]string 46 bgNormalColorTable [256]string 47 bgBrightColorTable [256]string 48 ) 49 50 func initFgNormalColorTable() { 51 for i := 0; i < 8; i++ { 52 s := strconv.Itoa(i + 30) 53 for j := 1 << i; j < 1<<(i+1); j++ { 54 fgNormalColorTable[j] = s 55 } 56 } 57 } 58 59 func initFgBrightColorTable() { 60 for i := 0; i < 8; i++ { 61 s := strconv.Itoa(i + 90) 62 for j := 1 << i; j < 1<<(i+1); j++ { 63 fgBrightColorTable[j] = s 64 } 65 } 66 } 67 68 func initBgNormalColorTable() { 69 for i := 0; i < 8; i++ { 70 s := strconv.Itoa(i + 40) 71 for j := 1 << i; j < 1<<(i+1); j++ { 72 bgNormalColorTable[j] = s 73 } 74 } 75 } 76 77 func initBgBrightColorTable() { 78 for i := 0; i < 8; i++ { 79 s := strconv.Itoa(i + 100) 80 for j := 1 << i; j < 1<<(i+1); j++ { 81 bgBrightColorTable[j] = s 82 } 83 } 84 } 85 86 func init() { 87 initFgNormalColorTable() 88 initFgBrightColorTable() 89 initBgNormalColorTable() 90 initBgBrightColorTable() 91 }