Skip to content

Commit 0ec8e78

Browse files
committed
chore: add more docs for detect color level on readme
1 parent d305f5f commit 0ec8e78

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed

README.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,9 @@ rgb.Println("rgb color")
456456
rgb.C256().Println("256 color")
457457
```
458458

459-
### Convert utils:
459+
### Convert utils
460460

461-
- `HexToRgb(hex string) (rgb []int)` Convert hex color string to RGB numbers
462-
- `RgbToHex(rgb []int) string` Convert RGB to hex code
461+
`color` has many built-in color conversion utility functions.
463462

464463
```go
465464
func Basic2hex(val uint8) string
@@ -513,6 +512,29 @@ There are some useful functions reference
513512

514513
> More useful func please see https://pkg.go.dev/github.com/gookit/color
515514

515+
### Detect color level
516+
517+
`color` automatically checks the color levels supported by the current environment.
518+
519+
```go
520+
// Level is the color level supported by a terminal.
521+
type Level = terminfo.ColorLevel
522+
523+
// terminal color available level alias of the terminfo.ColorLevel*
524+
const (
525+
LevelNo = terminfo.ColorLevelNone // not support color.
526+
Level16 = terminfo.ColorLevelBasic // basic - 3/4 bit color supported
527+
Level256 = terminfo.ColorLevelHundreds // hundreds - 8-bit color supported
528+
LevelRgb = terminfo.ColorLevelMillions // millions - (24 bit)true color supported
529+
)
530+
```
531+
532+
- `func SupportColor() bool` Whether the current environment supports color output
533+
- `func Support256Color() bool` Whether the current environment supports 256-color output
534+
- `func SupportTrueColor() bool` Whether the current environment supports (RGB)True-color output
535+
- `func TermColorLevel() Level` Get the currently supported color level
536+
537+
516538
## Projects using color
517539

518540
Check out these projects, which use https://github.com/gookit/color :

README.zh-CN.md

+61-7
Original file line numberDiff line numberDiff line change
@@ -469,29 +469,83 @@ rgb.Println("rgb color")
469469
rgb.C256().Println("256 color")
470470
```
471471

472-
**更多转换方法转换为 `RGBColor`**:
472+
### 颜色转换方法
473+
474+
`color` 内置了许多颜色转换工具方法
475+
476+
```go
477+
func Basic2hex(val uint8) string
478+
479+
func Bg2Fg(val uint8) uint8
480+
func Fg2Bg(val uint8) uint8
481+
482+
func C256ToRgb(val uint8) (rgb []uint8)
483+
func C256ToRgbV1(val uint8) (rgb []uint8)
484+
485+
func Hex2basic(hex string, asBg ...bool) uint8
486+
func Hex2rgb(hex string) []int
487+
func HexToRGB(hex string) []int
488+
func HexToRgb(hex string) (rgb []int)
489+
490+
func HslIntToRgb(h, s, l int) (rgb []uint8)
491+
func HslToRgb(h, s, l float64) (rgb []uint8)
492+
func HsvToRgb(h, s, v int) (rgb []uint8)
493+
494+
func Rgb2ansi(r, g, b uint8, isBg bool) uint8
495+
func Rgb2basic(r, g, b uint8, isBg bool) uint8
496+
func Rgb2hex(rgb []int) string
497+
func Rgb2short(r, g, b uint8) uint8
498+
func RgbTo256(r, g, b uint8) uint8
499+
func RgbTo256Table() map[string]uint8
500+
func RgbToAnsi(r, g, b uint8, isBg bool) uint8
501+
func RgbToHex(rgb []int) string
502+
func RgbToHsl(r, g, b uint8) []float64
503+
func RgbToHslInt(r, g, b uint8) []int
504+
```
505+
506+
**转换为 `RGBColor`**:
473507

474508
- `func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor`
475509
- `func RGBFromString(rgb string, isBg ...bool) RGBColor`
476510
- `func HEX(hex string, isBg ...bool) RGBColor`
477511
- `func HSL(h, s, l float64, isBg ...bool) RGBColor`
478512
- `func HSLInt(h, s, l int, isBg ...bool) RGBColor`
479513

480-
## 方法参考
514+
## 工具方法参考
481515

482516
一些有用的工具方法参考
483517

484-
- `Disable()` disable color render
485-
- `SetOutput(io.Writer)` custom set the colored text output writer
486-
- `ForceOpenColor()` force open color render
518+
- `Disable()` 禁用颜色渲染输出
519+
- `SetOutput(io.Writer)` 自定义设置渲染后的彩色文本输出位置
520+
- `ForceOpenColor()` 强制开启颜色渲染
487521
- `ClearCode(str string) string` Use for clear color codes
488522
- `Colors2code(colors ...Color) string` Convert colors to code. return like "32;45;3"
489523
- `ClearTag(s string) string` clear all color html-tag for a string
490524
- `IsConsole(w io.Writer)` Determine whether w is one of stderr, stdout, stdin
491-
- `HexToRgb(hex string) (rgb []int)` Convert hex color string to RGB numbers
492-
- `RgbToHex(rgb []int) string` Convert RGB to hex code
493525
- 更多请查看文档 https://pkg.go.dev/github.com/gookit/color
494526

527+
### 检测支持的颜色级别
528+
529+
`color` 会自动检查当前环境支持的颜色级别
530+
531+
```go
532+
// Level is the color level supported by a terminal.
533+
type Level = terminfo.ColorLevel
534+
535+
// terminal color available level alias of the terminfo.ColorLevel*
536+
const (
537+
LevelNo = terminfo.ColorLevelNone // not support color.
538+
Level16 = terminfo.ColorLevelBasic // basic - 3/4 bit color supported
539+
Level256 = terminfo.ColorLevelHundreds // hundreds - 8-bit color supported
540+
LevelRgb = terminfo.ColorLevelMillions // millions - (24 bit)true color supported
541+
)
542+
```
543+
544+
- `func SupportColor() bool` 当前环境是否支持色彩输出
545+
- `func Support256Color() bool` 当前环境是否支持256色彩输出
546+
- `func SupportTrueColor() bool` 当前环境是否支持(RGB)True色彩输出
547+
- `func TermColorLevel() Level` 获取当前支持的颜色级别
548+
495549
## 使用Color的项目
496550

497551
看看这些使用了 https://github.com/gookit/color 的项目:

color.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ var (
6969
// supportColor = IsSupportColor()
7070
)
7171

72-
// TermColorLevel value on current ENV
72+
// TermColorLevel Get the currently supported color level
7373
func TermColorLevel() Level {
7474
return colorLevel
7575
}
7676

77-
// SupportColor on the current ENV
77+
// SupportColor Whether the current environment supports color output
7878
func SupportColor() bool {
7979
return colorLevel > terminfo.ColorLevelNone
8080
}
@@ -84,12 +84,12 @@ func SupportColor() bool {
8484
// return colorLevel > terminfo.ColorLevelNone
8585
// }
8686

87-
// Support256Color on the current ENV
87+
// Support256Color Whether the current environment supports 256-color output
8888
func Support256Color() bool {
8989
return colorLevel > terminfo.ColorLevelBasic
9090
}
9191

92-
// SupportTrueColor on the current ENV
92+
// SupportTrueColor Whether the current environment supports (RGB)True-color output
9393
func SupportTrueColor() bool {
9494
return colorLevel > terminfo.ColorLevelHundreds
9595
}
@@ -98,7 +98,7 @@ func SupportTrueColor() bool {
9898
* global settings
9999
*************************************************************/
100100

101-
// Set set console color attributes
101+
// Set console color attributes
102102
func Set(colors ...Color) (int, error) {
103103
code := Colors2code(colors...)
104104
err := SetTerminal(code)
@@ -200,18 +200,17 @@ func RenderCode(code string, args ...interface{}) string {
200200
// RenderWithSpaces Render code with spaces.
201201
// If the number of args is > 1, a space will be added between the args
202202
func RenderWithSpaces(code string, args ...interface{}) string {
203-
message := formatArgsForPrintln(args)
203+
msg := formatArgsForPrintln(args)
204204
if len(code) == 0 {
205-
return message
205+
return msg
206206
}
207207

208208
// disabled OR not support color
209209
if !Enable || !SupportColor() {
210-
return ClearCode(message)
210+
return ClearCode(msg)
211211
}
212212

213-
// return fmt.Sprintf(FullColorTpl, code, message)
214-
return StartSet + code + "m" + message + ResetSet
213+
return StartSet + code + "m" + msg + ResetSet
215214
}
216215

217216
// RenderString render a string with color code.
@@ -234,7 +233,8 @@ func RenderString(code string, str string) string {
234233

235234
// ClearCode clear color codes.
236235
//
237-
// eg: "\033[36;1mText\x1b[0m" -> "Text"
236+
// eg:
237+
// "\033[36;1mText\x1b[0m" -> "Text"
238238
func ClearCode(str string) string {
239239
return codeRegex.ReplaceAllString(str, "")
240240
}

0 commit comments

Comments
 (0)