2024-03-05 16:47:02 +00:00
|
|
|
package color
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Component is a type definition for the data type of a single color component.
|
|
|
|
type Component float32
|
|
|
|
|
|
|
|
// Color represents an RGB color.
|
|
|
|
type Color struct {
|
|
|
|
R Component
|
|
|
|
G Component
|
|
|
|
B Component
|
|
|
|
}
|
|
|
|
|
|
|
|
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
|
|
|
|
func RGB(r Component, g Component, b Component) 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)
|
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)
|
|
|
|
}
|