2020-05-28 13:21:20 +02:00
|
|
|
module gx
|
|
|
|
|
|
|
|
pub const (
|
2021-05-08 12:32:29 +02:00
|
|
|
blue = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 0
|
|
|
|
g: 0
|
|
|
|
b: 255
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
red = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 0
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
green = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 0
|
|
|
|
g: 255
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
yellow = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 255
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
orange = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 165
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
purple = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 128
|
|
|
|
g: 0
|
|
|
|
b: 128
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
black = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 0
|
|
|
|
g: 0
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
gray = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 128
|
|
|
|
g: 128
|
|
|
|
b: 128
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
indigo = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 75
|
|
|
|
g: 0
|
|
|
|
b: 130
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
pink = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 192
|
|
|
|
b: 203
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
violet = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 238
|
|
|
|
g: 130
|
|
|
|
b: 238
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
white = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 255
|
|
|
|
b: 255
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
dark_blue = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 0
|
|
|
|
g: 0
|
|
|
|
b: 139
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
dark_gray = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 169
|
|
|
|
g: 169
|
|
|
|
b: 169
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
dark_green = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 0
|
|
|
|
g: 100
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
dark_red = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 139
|
|
|
|
g: 0
|
|
|
|
b: 0
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
light_blue = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 173
|
|
|
|
g: 216
|
|
|
|
b: 230
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
light_gray = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 211
|
|
|
|
g: 211
|
|
|
|
b: 211
|
|
|
|
}
|
|
|
|
light_green = Color{
|
|
|
|
r: 144
|
|
|
|
g: 238
|
|
|
|
b: 144
|
|
|
|
}
|
2021-05-08 12:32:29 +02:00
|
|
|
light_red = Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: 255
|
|
|
|
g: 204
|
|
|
|
b: 203
|
|
|
|
}
|
2020-05-28 13:21:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Color represents a 32 bit color value in sRGB format
|
|
|
|
pub struct Color {
|
|
|
|
pub mut:
|
|
|
|
r byte
|
|
|
|
g byte
|
|
|
|
b byte
|
|
|
|
a byte = 255
|
|
|
|
}
|
|
|
|
|
|
|
|
// hex takes in a 32 bit integer and splits it into 4 byte values
|
|
|
|
pub fn hex(color int) Color {
|
2020-10-18 08:48:13 +02:00
|
|
|
return Color{
|
|
|
|
r: byte((color >> 24) & 0xFF)
|
|
|
|
g: byte((color >> 16) & 0xFF)
|
|
|
|
b: byte((color >> 8) & 0xFF)
|
2021-05-08 12:32:29 +02:00
|
|
|
a: byte(color & 0xFF)
|
2020-05-28 13:21:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 08:48:13 +02:00
|
|
|
pub fn rgb(r byte, g byte, b byte) Color {
|
2020-05-28 13:21:20 +02:00
|
|
|
return Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: r
|
|
|
|
g: g
|
2020-05-28 13:21:20 +02:00
|
|
|
b: b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 08:48:13 +02:00
|
|
|
pub fn rgba(r byte, g byte, b byte, a byte) Color {
|
2020-05-28 13:21:20 +02:00
|
|
|
return Color{
|
2020-10-18 08:48:13 +02:00
|
|
|
r: r
|
|
|
|
g: g
|
|
|
|
b: b
|
2020-05-28 13:21:20 +02:00
|
|
|
a: a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:40:03 +01:00
|
|
|
pub fn (c Color) + (c2 Color) Color {
|
2020-10-18 08:48:13 +02:00
|
|
|
return Color{
|
|
|
|
r: c.r + c2.r
|
|
|
|
g: c.g + c2.g
|
|
|
|
b: c.b + c2.b
|
2020-05-28 13:21:20 +02:00
|
|
|
a: c.b + c2.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:40:03 +01:00
|
|
|
pub fn (c Color) - (c2 Color) Color {
|
2020-10-18 08:48:13 +02:00
|
|
|
return Color{
|
|
|
|
r: c.r - c2.r
|
|
|
|
g: c.g - c2.g
|
|
|
|
b: c.b - c2.b
|
2020-05-28 13:21:20 +02:00
|
|
|
a: c.b - c2.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:40:03 +01:00
|
|
|
pub fn (c Color) * (c2 Color) Color {
|
2020-10-18 08:48:13 +02:00
|
|
|
return Color{
|
|
|
|
r: c.r * c2.r
|
|
|
|
g: c.g * c2.g
|
|
|
|
b: c.b * c2.b
|
2020-05-28 13:21:20 +02:00
|
|
|
a: c.b * c2.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:40:03 +01:00
|
|
|
pub fn (c Color) / (c2 Color) Color {
|
2020-10-18 08:48:13 +02:00
|
|
|
return Color{
|
|
|
|
r: c.r / c2.r
|
|
|
|
g: c.g / c2.g
|
|
|
|
b: c.b / c2.b
|
2020-05-28 13:21:20 +02:00
|
|
|
a: c.b / c2.a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c Color) eq(c2 Color) bool {
|
|
|
|
return c.r == c2.r && c.g == c2.g && c.b == c2.b && c.a == c2.a
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c Color) str() string {
|
|
|
|
return 'Color{$c.r, $c.g, $c.b, $c.a}'
|
|
|
|
}
|
2020-08-09 03:57:54 +02:00
|
|
|
|
2021-01-08 12:40:03 +01:00
|
|
|
// rgba8 - convert a color value to an int in the RGBA8 order.
|
|
|
|
// see https://developer.apple.com/documentation/coreimage/ciformat
|
|
|
|
[inline]
|
|
|
|
pub fn (c Color) rgba8() int {
|
|
|
|
return (int(c.r) << 24) + (int(c.g) << 16) + (int(c.b) << 8) + int(c.a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bgra8 - convert a color value to an int in the BGRA8 order.
|
|
|
|
// see https://developer.apple.com/documentation/coreimage/ciformat
|
|
|
|
[inline]
|
|
|
|
pub fn (c Color) bgra8() int {
|
|
|
|
return (int(c.b) << 24) + (int(c.g) << 16) + (int(c.r) << 8) + int(c.a)
|
|
|
|
}
|
|
|
|
|
2021-05-08 12:32:29 +02:00
|
|
|
// abgr8 - convert a color value to an int in the ABGR8 order.
|
2021-01-08 12:40:03 +01:00
|
|
|
// see https://developer.apple.com/documentation/coreimage/ciformat
|
|
|
|
[inline]
|
|
|
|
pub fn (c Color) abgr8() int {
|
|
|
|
return (int(c.a) << 24) + (int(c.b) << 16) + (int(c.g) << 8) + int(c.r)
|
|
|
|
}
|
|
|
|
|
2020-08-09 03:57:54 +02:00
|
|
|
const (
|
2021-05-08 12:32:29 +02:00
|
|
|
string_colors = map{
|
2021-07-03 09:09:11 +02:00
|
|
|
'blue': blue
|
|
|
|
'red': red
|
|
|
|
'green': green
|
|
|
|
'yellow': yellow
|
|
|
|
'orange': orange
|
|
|
|
'purple': purple
|
|
|
|
'black': black
|
|
|
|
'gray': gray
|
|
|
|
'indigo': indigo
|
|
|
|
'pink': pink
|
|
|
|
'violet': violet
|
|
|
|
'white': white
|
|
|
|
'dark_blue': dark_blue
|
|
|
|
'dark_gray': dark_gray
|
|
|
|
'dark_green': dark_green
|
|
|
|
'dark_red': dark_red
|
|
|
|
'light_blue': light_blue
|
|
|
|
'light_gray': light_gray
|
|
|
|
'light_green': light_green
|
|
|
|
'light_red': light_red
|
2020-10-18 08:48:13 +02:00
|
|
|
}
|
2020-08-09 03:57:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
pub fn color_from_string(s string) Color {
|
2021-05-08 12:32:29 +02:00
|
|
|
return gx.string_colors[s]
|
2020-08-09 03:57:54 +02:00
|
|
|
}
|