gx: add .rgba8(), .bgra8(), .abgr8() methods to gx.Color (#7911)
parent
a481c1785b
commit
cad4c5ee37
|
@ -139,7 +139,7 @@ pub fn rgba(r byte, g byte, b byte, a byte) Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c Color) +(c2 Color) Color {
|
pub fn (c Color) + (c2 Color) Color {
|
||||||
return Color{
|
return Color{
|
||||||
r: c.r + c2.r
|
r: c.r + c2.r
|
||||||
g: c.g + c2.g
|
g: c.g + c2.g
|
||||||
|
@ -148,7 +148,7 @@ pub fn (c Color) +(c2 Color) Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c Color) -(c2 Color) Color {
|
pub fn (c Color) - (c2 Color) Color {
|
||||||
return Color{
|
return Color{
|
||||||
r: c.r - c2.r
|
r: c.r - c2.r
|
||||||
g: c.g - c2.g
|
g: c.g - c2.g
|
||||||
|
@ -157,7 +157,7 @@ pub fn (c Color) -(c2 Color) Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c Color) *(c2 Color) Color {
|
pub fn (c Color) * (c2 Color) Color {
|
||||||
return Color{
|
return Color{
|
||||||
r: c.r * c2.r
|
r: c.r * c2.r
|
||||||
g: c.g * c2.g
|
g: c.g * c2.g
|
||||||
|
@ -166,7 +166,7 @@ pub fn (c Color) *(c2 Color) Color {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c Color) /(c2 Color) Color {
|
pub fn (c Color) / (c2 Color) Color {
|
||||||
return Color{
|
return Color{
|
||||||
r: c.r / c2.r
|
r: c.r / c2.r
|
||||||
g: c.g / c2.g
|
g: c.g / c2.g
|
||||||
|
@ -183,11 +183,32 @@ pub fn (c Color) str() string {
|
||||||
return 'Color{$c.r, $c.g, $c.b, $c.a}'
|
return 'Color{$c.r, $c.g, $c.b, $c.a}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// abgr8 - convert a color value to an int in the ABGR8 order.
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
string_colors = {
|
string_colors = {
|
||||||
'black': black
|
'black': black
|
||||||
'blue': blue
|
'blue': blue
|
||||||
'red': red
|
'red': red
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,47 +1,63 @@
|
||||||
import gx
|
import gx
|
||||||
|
|
||||||
|
|
||||||
fn test_hex() {
|
fn test_hex() {
|
||||||
// valid colors
|
// valid colors
|
||||||
a := gx.hex(0x6c5ce7ff)
|
a := gx.hex(0x6c5ce7ff)
|
||||||
b := gx.rgba(108, 92, 231, 255)
|
b := gx.rgba(108, 92, 231, 255)
|
||||||
|
assert a.eq(b)
|
||||||
assert a.eq(b)
|
// doesn't give right value with short hex value
|
||||||
|
short := gx.hex(0xfff)
|
||||||
// doesn't give right value with short hex value
|
assert !short.eq(gx.white)
|
||||||
short := gx.hex(0xfff)
|
|
||||||
|
|
||||||
assert !short.eq(gx.white)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_add() {
|
fn test_add() {
|
||||||
a := gx.rgba(100, 100, 100, 100)
|
a := gx.rgba(100, 100, 100, 100)
|
||||||
b := gx.rgba(100, 100, 100, 100)
|
b := gx.rgba(100, 100, 100, 100)
|
||||||
r := gx.rgba(200, 200, 200, 200)
|
r := gx.rgba(200, 200, 200, 200)
|
||||||
|
assert (a + b).eq(r)
|
||||||
assert (a + b).eq(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_sub() {
|
fn test_sub() {
|
||||||
a := gx.rgba(100, 100, 100, 100)
|
a := gx.rgba(100, 100, 100, 100)
|
||||||
b := gx.rgba(100, 100, 100, 100)
|
b := gx.rgba(100, 100, 100, 100)
|
||||||
r := gx.rgba(0, 0, 0, 0)
|
r := gx.rgba(0, 0, 0, 0)
|
||||||
|
assert (a - b).eq(r)
|
||||||
assert (a - b).eq(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_mult() {
|
fn test_mult() {
|
||||||
a := gx.rgba(10, 10, 10, 10)
|
a := gx.rgba(10, 10, 10, 10)
|
||||||
b := gx.rgba(10, 10, 10, 10)
|
b := gx.rgba(10, 10, 10, 10)
|
||||||
r := gx.rgba(100, 100, 100, 100)
|
r := gx.rgba(100, 100, 100, 100)
|
||||||
|
assert (a * b).eq(r)
|
||||||
assert (a * b).eq(r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_div() {
|
fn test_div() {
|
||||||
a := gx.rgba(100, 100, 100, 100)
|
a := gx.rgba(100, 100, 100, 100)
|
||||||
b := gx.rgba(10, 10, 10, 10)
|
b := gx.rgba(10, 10, 10, 10)
|
||||||
r := gx.rgba(10, 10, 10, 10)
|
r := gx.rgba(10, 10, 10, 10)
|
||||||
|
assert (a / b).eq(r)
|
||||||
assert (a / b).eq(r)
|
}
|
||||||
|
|
||||||
|
fn test_rgba8() {
|
||||||
|
assert gx.white.rgba8() == -1
|
||||||
|
assert gx.black.rgba8() == 255
|
||||||
|
assert gx.red.rgba8() == -16776961
|
||||||
|
assert gx.green.rgba8() == 16711935
|
||||||
|
assert gx.blue.rgba8() == 65535
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_bgra8() {
|
||||||
|
assert gx.white.bgra8() == -1
|
||||||
|
assert gx.black.bgra8() == 255
|
||||||
|
assert gx.red.bgra8() == 65535
|
||||||
|
assert gx.green.bgra8() == 16711935
|
||||||
|
assert gx.blue.bgra8() == -16776961
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_abgr8() {
|
||||||
|
assert gx.white.abgr8() == -1
|
||||||
|
assert gx.black.abgr8() == -16777216
|
||||||
|
assert gx.red.abgr8() == -16776961
|
||||||
|
assert gx.green.abgr8() == -16711936
|
||||||
|
assert gx.blue.abgr8() == -65536
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue