Skip to content

Commit

Permalink
[color] Added method 'Term2RGB'
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Dec 12, 2023
1 parent ef2173f commit f8bfdd7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.92.0

* `[color]` Added method `Term2RGB`

### 12.91.0

* `[terminal/tty]` Added method `IsTMUX`
Expand Down
70 changes: 68 additions & 2 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ func Hex2RGBA(h Hex) RGBA {
return RGBA{uint8(h.v>>16) & 0xFF, uint8(h.v>>8) & 0xFF, uint8(h.v) & 0xFF, 0}
}

// RGB2Term convert rgb color to terminal color code
// RGB2Term converts RGB color to terminal color code
// https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1
func RGB2Term(c RGB) int {
R, G, B := int(c.R), int(c.G), int(c.B)

// grayscale
// Grayscale
if R == G && G == B {
if R == 175 {
return 145
Expand All @@ -455,6 +455,72 @@ func RGB2Term(c RGB) int {
return 36*(R/51) + 6*(G/51) + (B / 51) + 16
}

// Term2RGB converts terminal color code (0-255) to RGB color
// https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1
func Term2RGB(c uint8) RGB {
// Grayscale
if c > 231 {
c = ((c - 232) * 10) + 8
return RGB{c, c, c}
}

// Default colors (not standardized, average values)
switch c {
case 0:
return RGB{}
case 1:
return RGB{255, 0, 0}
case 2:
return RGB{0, 255, 0}
case 3:
return RGB{255, 255, 0}
case 4:
return RGB{0, 0, 255}
case 5:
return RGB{255, 0, 255}
case 6:
return RGB{0, 255, 255}
case 7:
return RGB{191, 191, 191}
case 8:
return RGB{64, 64, 64}
case 9:
return RGB{255, 127, 127}
case 10:
return RGB{127, 255, 127}
case 11:
return RGB{255, 255, 127}
case 12:
return RGB{127, 127, 255}
case 13:
return RGB{255, 127, 255}
case 14:
return RGB{127, 255, 255}
case 15:
return RGB{127, 127, 127}
}

var r, g, b, ir, ig, ib uint8

ir = (c - 16) / 36
ig = ((c - 16) % 36) / 6
ib = (c - 16) % 6

if ir > 0 {
r = (ir * 40) + 55
}

if ig > 0 {
g = (ig * 40) + 55
}

if ib > 0 {
b = (ib * 40) + 55
}

return RGB{r, g, b}
}

// RGB2CMYK converts RGB color to CMYK
func RGB2CMYK(c RGB) CMYK {
R, G, B := float64(c.R)/255.0, float64(c.G)/255.0, float64(c.B)/255.0
Expand Down
22 changes: 21 additions & 1 deletion color/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,31 @@ func (s *ColorSuite) TestTerm(c *C) {
c.Assert(RGB{0, 175, 175}.ToTerm(), Equals, 37)
c.Assert(RGB{255, 255, 0}.ToTerm(), Equals, 226)
c.Assert(RGB{135, 175, 215}.ToTerm(), Equals, 110)
// grayscale
c.Assert(RGB{175, 175, 175}.ToTerm(), Equals, 145)
c.Assert(RGB{18, 18, 18}.ToTerm(), Equals, 233)
c.Assert(RGB{48, 48, 48}.ToTerm(), Equals, 236)
c.Assert(RGB{238, 238, 238}.ToTerm(), Equals, 255)

c.Assert(Term2RGB(0), DeepEquals, RGB{0, 0, 0})
c.Assert(Term2RGB(1), DeepEquals, RGB{255, 0, 0})
c.Assert(Term2RGB(2), DeepEquals, RGB{0, 255, 0})
c.Assert(Term2RGB(3), DeepEquals, RGB{255, 255, 0})
c.Assert(Term2RGB(4), DeepEquals, RGB{0, 0, 255})
c.Assert(Term2RGB(5), DeepEquals, RGB{255, 0, 255})
c.Assert(Term2RGB(6), DeepEquals, RGB{0, 255, 255})
c.Assert(Term2RGB(7), DeepEquals, RGB{191, 191, 191})
c.Assert(Term2RGB(8), DeepEquals, RGB{64, 64, 64})
c.Assert(Term2RGB(9), DeepEquals, RGB{255, 127, 127})
c.Assert(Term2RGB(10), DeepEquals, RGB{127, 255, 127})
c.Assert(Term2RGB(11), DeepEquals, RGB{255, 255, 127})
c.Assert(Term2RGB(12), DeepEquals, RGB{127, 127, 255})
c.Assert(Term2RGB(13), DeepEquals, RGB{255, 127, 255})
c.Assert(Term2RGB(14), DeepEquals, RGB{127, 255, 255})
c.Assert(Term2RGB(15), DeepEquals, RGB{127, 127, 127})

c.Assert(Term2RGB(238), DeepEquals, RGB{68, 68, 68})
c.Assert(Term2RGB(153), DeepEquals, RGB{175, 215, 255})

}

func (s *ColorSuite) TestLuminance(c *C) {
Expand Down
8 changes: 8 additions & 0 deletions color/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func ExampleRGB2Term() {
// Output: RGB{R:255 G:0 B:0} → \e[38;5;196m
}

func ExampleTerm2RGB() {
c := uint8(162)

fmt.Printf("%d → %s\n", c, Term2RGB(c))

// Output: 162 → RGB{R:215 G:0 B:135}
}

func ExampleRGB2CMYK() {
fmt.Printf("%s\n", RGB2CMYK(RGB{127, 25, 75}))

Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.91.0"
const VERSION = "12.92.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down

0 comments on commit f8bfdd7

Please sign in to comment.