2024-03-05 19:10:27 +01:00

52 lines
1.1 KiB
Go

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)
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)
}