2024-03-05 16:47:02 +00:00
|
|
|
package color
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2024-03-05 23:18:51 +00:00
|
|
|
// Value is a type definition for the data type of a single color component.
|
|
|
|
type Value = float64
|
2024-03-05 16:47:02 +00:00
|
|
|
|
|
|
|
// Color represents an RGB color.
|
|
|
|
type Color struct {
|
2024-03-05 23:18:51 +00:00
|
|
|
R Value
|
|
|
|
G Value
|
|
|
|
B Value
|
2024-03-05 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
|
2024-03-05 23:18:51 +00:00
|
|
|
func RGB(r Value, g Value, b Value) Color {
|
2024-03-05 16:47:02 +00:00
|
|
|
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)
|
2024-03-05 18:10:27 +00:00
|
|
|
return
|
2024-03-05 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-03-05 18:10:27 +00:00
|
|
|
return
|
2024-03-05 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-03-05 18:10:27 +00:00
|
|
|
return
|
2024-03-05 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(formatLine, byte(c.R*255), byte(c.G*255), byte(c.B*255), text)
|
|
|
|
}
|