Fix gradients to use rgbx.

This commit is contained in:
treeform 2021-02-25 16:26:55 -08:00
parent 22aace7a5e
commit 076dc60468
9 changed files with 14 additions and 14 deletions

View file

@ -11,8 +11,8 @@ let paint = Paint(
vec2(100, 200) vec2(100, 200)
], ],
gradientStops: @[ gradientStops: @[
ColorStop(color: rgba(255, 0, 0, 255).color, position: 0), ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40).color, position: 1.0), ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
] ]
) )
@ -28,4 +28,4 @@ image.fillPath(
paint paint
) )
image.writeFile("examples/paint.png") image.writeFile("examples/gradient.png")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View file

@ -24,7 +24,7 @@ type
ColorStop* = object ColorStop* = object
## Color stop on a gradient curve. ## Color stop on a gradient curve.
color*: Color ## Color of the stop color*: ColorRGBX ## Color of the stop
position*: float32 ## Gradient Stop position 0..1. position*: float32 ## Gradient Stop position 0..1.
proc toLineSpace(at, to, point: Vec2): float32 = proc toLineSpace(at, to, point: Vec2): float32 =
@ -46,17 +46,17 @@ proc gradientPut(image: Image, x, y: int, a: float32, stops: seq[ColorStop]) =
var color: Color var color: Color
if index == -1: if index == -1:
# first stop solid # first stop solid
color = stops[0].color color = stops[0].color.color
elif index + 1 >= stops.len: elif index + 1 >= stops.len:
# last stop solid # last stop solid
color = stops[index].color color = stops[index].color.color
else: else:
let let
gs1 = stops[index] gs1 = stops[index]
gs2 = stops[index+1] gs2 = stops[index+1]
color = mix( color = mix(
gs1.color, gs1.color.color,
gs2.color, gs2.color.color,
(a - gs1.position) / (gs2.position - gs1.position) (a - gs1.position) / (gs2.position - gs1.position)
) )
image.setRgbaUnsafe(x, y, color.rgba.toPremultipliedAlpha()) image.setRgbaUnsafe(x, y, color.rgba.toPremultipliedAlpha())

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View file

@ -58,8 +58,8 @@ block:
vec2(100, 50), vec2(100, 50),
], ],
gradientStops: @[ gradientStops: @[
ColorStop(color: rgba(255, 0, 0, 255).color, position: 0), ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40).color, position: 1.0), ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
] ]
) )
) )
@ -78,8 +78,8 @@ block:
vec2(50, 100) vec2(50, 100)
], ],
gradientStops: @[ gradientStops: @[
ColorStop(color: rgba(255, 0, 0, 255).color, position: 0), ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40).color, position: 1.0), ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
] ]
) )
) )
@ -99,8 +99,8 @@ block:
vec2(50, 100) vec2(50, 100)
], ],
gradientStops: @[ gradientStops: @[
ColorStop(color: rgba(255, 0, 0, 255).color, position: 0), ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40).color, position: 1.0), ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
] ]
) )
) )