52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package color
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// Value is a type definition for the data type of a single color component.
|
|
type Value = float64
|
|
|
|
// Color represents an RGB color.
|
|
type Color struct {
|
|
R Value
|
|
G Value
|
|
B Value
|
|
}
|
|
|
|
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
|
|
func RGB(r Value, g Value, b Value) Color {
|
|
return Color{r, g, b}
|
|
}
|
|
|
|
// Fprint writes the text in the given color to the writer.
|
|
func (c Color) Fprint(writer io.Writer, text string) {
|
|
if !Terminal {
|
|
fmt.Fprint(writer, text)
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(writer, format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
|
|
}
|
|
|
|
// Print writes the text in the given color to standard output.
|
|
func (c Color) Print(text string) {
|
|
if !Terminal {
|
|
fmt.Print(text)
|
|
return
|
|
}
|
|
|
|
fmt.Printf(format, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
|
|
}
|
|
|
|
// Println writes the text in the given color to standard output and appends a newline.
|
|
func (c Color) Println(text string) {
|
|
if !Terminal {
|
|
fmt.Println(text)
|
|
return
|
|
}
|
|
|
|
fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
|
|
}
|