Skip to content

Commit

Permalink
complete all functions. add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 23, 2018
1 parent 8d7dc75 commit 801a2ac
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 92 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
[![Coverage Status](https://coveralls.io/repos/github/gookit/color/badge.svg?branch=master)](https://coveralls.io/github/gookit/color?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/color)](https://goreportcard.com/report/github.com/gookit/color)

Command line color library, written using golang
Command line color library. rich color rendering output, universal API method, compatible with Windows system

**[中文说明](README_cn.md)**

Expand Down
5 changes: 0 additions & 5 deletions _examples/256color.go

This file was deleted.

56 changes: 56 additions & 0 deletions _examples/color256.go
@@ -0,0 +1,56 @@
package main

import (
"github.com/gookit/color"
"fmt"
)

// go run ./_examples/color256.go
func main() {
// var s *color.Style256

fmt.Printf("%-45s 256 Color(16 bit) Table %-35s\n", " ", " ")
// 0 - 16
fmt.Printf("%-22sStandard Color %-42sExtended Color \n", " ", " ")
for i := range []int{7:0} {// 0 -7 -> 8/16color: 30–37
color.S256(255, uint8(i)).Printf(" %-4d", i)
}
fmt.Print(" ")
for i := range []int{7:0} {// 8 -15 -> 16color: 90–97
i += 8
color.S256(0, uint8(i)).Printf(" %-4d", i)
}

var fg uint8 = 255
fmt.Printf("\n%-50s216 Color\n", " ")
for i := range []int{215:0} {// 16-231:6 × 6 × 6 立方(216色): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
v := i + 16

if i != 0 {
if i%18 == 0 {
fg = 0
fmt.Println() // new line
}

if i % 36 == 0 {
fg = 255
// fmt.Println() // new line
}
}

color.S256(fg, uint8(v)).Printf(" %-4d", v)
}

fmt.Printf("\n%-50s24th Order Grayscale Color\n", " ")
for i := range []int{23:0} {// // 232-255:从黑到白的24阶灰度色
if i < 12 {
fg = 255
} else {
fg = 0
}

i += 232
color.S256(fg, uint8(i)).Printf(" %-4d", i)
}
fmt.Println()
}
Binary file added _examples/images/256-color.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Expand Up @@ -61,7 +61,7 @@

当硬件开始使用8位DAC时,多个软件为这些颜色名称分配了24位的代码。下面的图表显示了发送到DAC的一些常用硬件和软件的值。

![cmd-term-colors](../images/cmd-term-colors.jpg)
![cmd-term-colors](4bit-colors.jpg)

### 8位

Expand All @@ -77,7 +77,7 @@
232-255:从黑到白的24阶灰度色
```

![8-byte-colors](../images/8-byte-colors.jpg)
![8-byte-colors](8bit-colors.jpg)

### 24位

Expand Down
63 changes: 59 additions & 4 deletions color.go
@@ -1,5 +1,6 @@
/*
Package color is command line color library, written using golang
Package color is Command line color library.
Support rich color rendering output, universal API method, compatible with Windows system
Source code and other details for the project are available at GitHub:
Expand All @@ -16,9 +17,9 @@ import (

// console color mode
const (
ModeNormal = iota
Mode256 // 8 bite
ModeRGB // 24 bite
ModeNormal = iota
Mode256 // 8 bite
ModeRGB // 24 bite
ModeGrayscale
)

Expand Down Expand Up @@ -131,3 +132,57 @@ func RenderString(code string, str string) string {
func ClearCode(str string) string {
return codeRegex.ReplaceAllString(str, "")
}

/*************************************************************
* colored message Printer
*************************************************************/

// PrinterFace interface
type PrinterFace interface {
fmt.Stringer
Sprint(a ...interface{}) string
Sprintf(format string, a ...interface{}) string
Print(a ...interface{})
Printf(format string, a ...interface{})
Println(a ...interface{})
}

// Printer a generic color message printer.
// Usage:
// p := &Printer{"32;45;3"}
// p.Print("message")
type Printer struct {
// ColorCode color code string. eg "32;45;3"
ColorCode string
}

// String returns color code string. eg: "32;45;3"
func (p *Printer) String() string {
// panic("implement me")
return p.ColorCode
}

// Sprint returns rendering colored messages
func (p *Printer) Sprint(a ...interface{}) string {
return RenderCode(p.String(), a...)
}

// Sprintf returns format and rendering colored messages
func (p *Printer) Sprintf(format string, a ...interface{}) string {
return RenderString(p.String(), fmt.Sprintf(format, a...))
}

// Print rendering colored messages
func (p *Printer) Print(a ...interface{}) {
fmt.Print(RenderCode(p.String(), a...))
}

// Printf format and rendering colored messages
func (p *Printer) Printf(format string, a ...interface{}) {
fmt.Print(RenderString(p.String(), fmt.Sprintf(format, a...)))
}

// Println rendering colored messages with newline
func (p *Printer) Println(a ...interface{}) {
fmt.Println(RenderCode(p.String(), a...))
}
27 changes: 6 additions & 21 deletions color_256.go
Expand Up @@ -6,7 +6,7 @@ import (
)

/*
from wikipedia:
from wikipedia, 256 color:
ESC[ … 38;5;<n> … m选择前景色
ESC[ … 48;5;<n> … m选择背景色
0- 7:标准颜色(同 ESC[30–37m)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c Color256) Value() uint8 {
return c[0]
}

// String convert to string
// String convert to color code string.
func (c Color256) String() string {
if c[1] == AsFg { // 0 is Fg
return fmt.Sprintf(TplFg256, c[0])
Expand Down Expand Up @@ -125,7 +125,10 @@ func (c Color256) IsEmpty() bool {
// 都是由两位uint8组成, 第一位是色彩值;
// 第二位与Bit8Color不一样的是,在这里表示是否设置了值 0 未设置 ^0 已设置
type Style256 struct {
Name string
p *Printer
// Name of the style
Name string
// fg and bg color
fg, bg Color256
}

Expand Down Expand Up @@ -205,21 +208,3 @@ func (s *Style256) String() string {

return strings.Join(ss, ";")
}

// Color256Table display
func Color256Table() {

}

// RGBto216 16-231:6 × 6 × 6 立方(216色): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
func RGBto216(n int) int {
if n < 0 {
return 0
}

if n > 5 {
return 5
}

return n
}

0 comments on commit 801a2ac

Please sign in to comment.