mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gx: implement C1.over(C2), add gx.cyan and gx.magenta, fix + and - ops, add more tests
This commit is contained in:
114
vlib/gx/color.v
114
vlib/gx/color.v
@@ -1,9 +1,19 @@
|
||||
module gx
|
||||
|
||||
pub const (
|
||||
blue = Color{
|
||||
black = Color{
|
||||
r: 0
|
||||
g: 0
|
||||
b: 0
|
||||
}
|
||||
gray = Color{
|
||||
r: 128
|
||||
g: 128
|
||||
b: 128
|
||||
}
|
||||
white = Color{
|
||||
r: 255
|
||||
g: 255
|
||||
b: 255
|
||||
}
|
||||
red = Color{
|
||||
@@ -16,11 +26,26 @@ pub const (
|
||||
g: 255
|
||||
b: 0
|
||||
}
|
||||
blue = Color{
|
||||
r: 0
|
||||
g: 0
|
||||
b: 255
|
||||
}
|
||||
yellow = Color{
|
||||
r: 255
|
||||
g: 255
|
||||
b: 0
|
||||
}
|
||||
magenta = Color{
|
||||
r: 255
|
||||
g: 0
|
||||
b: 255
|
||||
}
|
||||
cyan = Color{
|
||||
r: 0
|
||||
g: 255
|
||||
b: 255
|
||||
}
|
||||
orange = Color{
|
||||
r: 255
|
||||
g: 165
|
||||
@@ -31,16 +56,6 @@ pub const (
|
||||
g: 0
|
||||
b: 128
|
||||
}
|
||||
black = Color{
|
||||
r: 0
|
||||
g: 0
|
||||
b: 0
|
||||
}
|
||||
gray = Color{
|
||||
r: 128
|
||||
g: 128
|
||||
b: 128
|
||||
}
|
||||
indigo = Color{
|
||||
r: 75
|
||||
g: 0
|
||||
@@ -56,11 +71,6 @@ pub const (
|
||||
g: 130
|
||||
b: 238
|
||||
}
|
||||
white = Color{
|
||||
r: 255
|
||||
g: 255
|
||||
b: 255
|
||||
}
|
||||
dark_blue = Color{
|
||||
r: 0
|
||||
g: 0
|
||||
@@ -139,21 +149,55 @@ pub fn rgba(r byte, g byte, b byte, a byte) Color {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (c Color) + (c2 Color) Color {
|
||||
// + adds `b` to `a`, with a maximum value of 255 for each channel
|
||||
pub fn (a Color) + (b Color) Color {
|
||||
mut na := int(a.a) + b.a
|
||||
mut nr := int(a.r) + b.r
|
||||
mut ng := int(a.g) + b.g
|
||||
mut nb := int(a.b) + b.b
|
||||
if na > 255 {
|
||||
na = 255
|
||||
}
|
||||
if nr > 255 {
|
||||
nr = 255
|
||||
}
|
||||
if ng > 255 {
|
||||
ng = 255
|
||||
}
|
||||
if nb > 255 {
|
||||
nb = 255
|
||||
}
|
||||
return Color{
|
||||
r: c.r + c2.r
|
||||
g: c.g + c2.g
|
||||
b: c.b + c2.b
|
||||
a: c.a + c2.a
|
||||
r: byte(nr)
|
||||
g: byte(ng)
|
||||
b: byte(nb)
|
||||
a: byte(na)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (c Color) - (c2 Color) Color {
|
||||
// - subtracts `b` from `a`, with a minimum value of 0 for each channel
|
||||
pub fn (a Color) - (b Color) Color {
|
||||
mut na := if a.a > b.a { a.a } else { b.a }
|
||||
mut nr := int(a.r) - b.r
|
||||
mut ng := int(a.g) - b.g
|
||||
mut nb := int(a.b) - b.b
|
||||
if na < 0 {
|
||||
na = 0
|
||||
}
|
||||
if nr < 0 {
|
||||
nr = 0
|
||||
}
|
||||
if ng < 0 {
|
||||
ng = 0
|
||||
}
|
||||
if nb < 0 {
|
||||
nb = 0
|
||||
}
|
||||
return Color{
|
||||
r: c.r - c2.r
|
||||
g: c.g - c2.g
|
||||
b: c.b - c2.b
|
||||
a: c.a - c2.a
|
||||
r: byte(nr)
|
||||
g: byte(ng)
|
||||
b: byte(nb)
|
||||
a: byte(na)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,6 +219,24 @@ pub fn (c Color) / (c2 Color) Color {
|
||||
}
|
||||
}
|
||||
|
||||
// over - implements an `a` over `b` operation.
|
||||
// see https://keithp.com/~keithp/porterduff/p253-porter.pdf
|
||||
pub fn (a Color) over(b Color) Color {
|
||||
aa := f32(a.a) / 255
|
||||
ab := f32(b.a) / 255
|
||||
ar := aa + ab * (1 - aa)
|
||||
//
|
||||
rr := (f32(a.r) * aa + f32(b.r) * ab * (1 - aa)) / ar
|
||||
gr := (f32(a.g) * aa + f32(b.g) * ab * (1 - aa)) / ar
|
||||
br := (f32(a.b) * aa + f32(b.b) * ab * (1 - aa)) / ar
|
||||
return Color{
|
||||
r: byte(rr)
|
||||
g: byte(gr)
|
||||
b: byte(br)
|
||||
a: byte(ar * 255)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user