paint uses color

This commit is contained in:
Ryan Oldenburg 2021-08-11 22:54:55 -05:00
parent 0765c61c00
commit 8c458d2ab2
27 changed files with 62 additions and 59 deletions

View file

@ -129,18 +129,18 @@ image.fillText(font.typeset(text, bounds = vec2(180, 180)), vec2(10, 10))
```nim
let typeface = readTypeface("tests/fonts/Ubuntu-Regular_1.ttf")
proc newFont(typeface: Typeface, size: float32, color: ColorRGBA): Font =
proc newFont(typeface: Typeface, size: float32, color: Color): Font =
result = newFont(typeface)
result.size = size
result.paint.color = color
let spans = @[
newSpan("verb [with object] ",
newFont(typeface, 12, rgba(200, 200, 200, 255))),
newSpan("strallow\n", newFont(typeface, 36, rgba(0, 0, 0, 255))),
newSpan("\nstral·low\n", newFont(typeface, 13, rgba(0, 127, 244, 255))),
newFont(typeface, 12, color(0.78125, 0.78125, 0.78125, 1))),
newSpan("strallow\n", newFont(typeface, 36, color(0, 0, 0, 1))),
newSpan("\nstral·low\n", newFont(typeface, 13, color(0, 0.5, 0.953125, 1))),
newSpan("\n1. free (something) from restrictive restrictions \"the regulations are intended to strallow changes in public policy\" ",
newFont(typeface, 14, rgba(80, 80, 80, 255)))
newFont(typeface, 14, color(0.3125, 0.3125, 0.3125, 1)))
]
image.fillText(typeset(spans, bounds = vec2(180, 180)), vec2(10, 10))
@ -243,8 +243,8 @@ paint.gradientHandlePositions = @[
vec2(100, 200)
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
image.fillPath(

View file

@ -10,8 +10,8 @@ paint.gradientHandlePositions = @[
vec2(100, 200)
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
image.fillPath(

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 10 KiB

View file

@ -5,18 +5,18 @@ image.fill(rgba(255, 255, 255, 255))
let typeface = readTypeface("tests/fonts/Ubuntu-Regular_1.ttf")
proc newFont(typeface: Typeface, size: float32, color: ColorRGBA): Font =
proc newFont(typeface: Typeface, size: float32, color: Color): Font =
result = newFont(typeface)
result.size = size
result.paint.color = color
let spans = @[
newSpan("verb [with object] ",
newFont(typeface, 12, rgba(200, 200, 200, 255))),
newSpan("strallow\n", newFont(typeface, 36, rgba(0, 0, 0, 255))),
newSpan("\nstral·low\n", newFont(typeface, 13, rgba(0, 127, 244, 255))),
newFont(typeface, 12, color(0.78125, 0.78125, 0.78125, 1))),
newSpan("strallow\n", newFont(typeface, 36, color(0, 0, 0, 1))),
newSpan("\nstral·low\n", newFont(typeface, 13, color(0, 0.5, 0.953125, 1))),
newSpan("\n1. free (something) from restrictive restrictions \"the regulations are intended to strallow changes in public policy\" ",
newFont(typeface, 14, rgba(80, 80, 80, 255)))
newFont(typeface, 14, color(0.3125, 0.3125, 0.3125, 1)))
]
image.fillText(typeset(spans, bounds = vec2(180, 180)), vec2(10, 10))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -315,19 +315,19 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
proc fill(img: Image, ctx: Ctx, path: Path) {.inline.} =
if ctx.display and ctx.opacity > 0:
var paint = ctx.fill
let paint = newPaint(ctx.fill)
if ctx.opacity != 1:
paint.opacity = paint.opacity * ctx.opacity
img.fillPath(path, paint, ctx.transform, ctx.fillRule)
proc stroke(img: Image, ctx: Ctx, path: Path) {.inline.} =
if ctx.display and ctx.opacity > 0:
var color = ctx.stroke
let paint = newPaint(ctx.stroke)
if ctx.opacity != 1:
color = color.applyOpacity(ctx.opacity * ctx.strokeOpacity)
paint.color.a *= (ctx.opacity * ctx.strokeOpacity)
img.strokePath(
path,
color,
paint,
ctx.transform,
ctx.strokeWidth,
ctx.strokeLineCap,
@ -532,7 +532,7 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
)
linearGradient.stops.add(ColorStop(
color: color.parseHtmlColor().rgbx(),
color: color.parseHtmlColor(),
position: parseFloat(child.attr("offset"))
))
else:

View file

@ -876,11 +876,11 @@ proc shadow*(
mask = image.newMask()
shifted = newMask(mask.width, mask.height)
shifted.draw(mask, translate(offset), bmOverwrite)
mask.spread(spread)
mask.blur(blur)
result = newImage(mask.width, mask.height)
shifted.spread(spread)
shifted.blur(blur)
result = newImage(shifted.width, shifted.height)
result.fill(color)
result.draw(mask, blendMode = bmMask)
result.draw(shifted, blendMode = bmMask)
when defined(release):
{.pop.}

View file

@ -1,4 +1,4 @@
import blends, chroma, common, images, internal, vmath
import blends, chroma, common, images, vmath
type
PaintKind* = enum
@ -15,7 +15,7 @@ type
blendMode*: BlendMode ## Blend mode.
opacity*: float32
# pkSolid
color*: ColorRGBX ## Color to fill with.
color*: Color ## Color to fill with.
# pkImage, pkImageTiled:
image*: Image ## Image to fill with.
imageMat*: Mat3 ## Matrix of the filled image.
@ -25,7 +25,7 @@ type
ColorStop* = object
## Color stop on a gradient curve.
color*: ColorRGBX ## Color of the stop.
color*: Color ## Color of the stop.
position*: float32 ## Gradient stop position 0..1.
SomePaint* = string | Paint | SomeColor
@ -49,13 +49,13 @@ converter parseSomePaint*(paint: SomePaint): Paint {.inline.} =
## Given SomePaint, parse it in different ways.
when type(paint) is string:
result = newPaint(pkSolid)
result.color = parseHtmlColor(paint).rgbx()
result.color = parseHtmlColor(paint)
elif type(paint) is SomeColor:
result = newPaint(pkSolid)
when type(paint) is ColorRGBX:
when type(paint) is Color:
result.color = paint
else:
result.color = paint.rgbx()
result.color = paint.color()
elif type(paint) is Paint:
paint
@ -76,7 +76,7 @@ proc gradientPut(
index = i
if stop.position > t:
break
var color: ColorRGBX
var color: Color
if index == -1:
# first stop solid
color = stops[0].color
@ -92,9 +92,8 @@ proc gradientPut(
gs2.color,
(t - gs1.position) / (gs2.position - gs1.position)
)
if paint.opacity != 1:
color = color.applyOpacity(paint.opacity)
image.setRgbaUnsafe(x, y, color.rgba.rgbx())
color.a *= paint.opacity
image.setRgbaUnsafe(x, y, color.rgbx())
proc fillGradientLinear(image: Image, paint: Paint) =
## Fills a linear gradient.

View file

@ -1791,7 +1791,7 @@ proc fillPath*(
shapes.transform(transform)
var color = paint.color
if paint.opacity != 1:
color = color.applyOpacity(paint.opacity)
color.a *= paint.opacity
image.fillShapes(shapes, color, windingRule, paint.blendMode)
return
@ -1875,7 +1875,7 @@ proc strokePath*(
strokeShapes.transform(transform)
var color = paint.color
if paint.opacity != 1:
color = color.applyOpacity(paint.opacity)
color.a *= paint.opacity
image.fillShapes(strokeShapes, color, wrNonZero, paint.blendMode)
return

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 783 KiB

After

Width:  |  Height:  |  Size: 783 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 MiB

After

Width:  |  Height:  |  Size: 3.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 MiB

After

Width:  |  Height:  |  Size: 3.9 MiB

View file

@ -48,10 +48,12 @@ proc renderEmojiSet(index: int) =
for i in 0 ..< rows:
for j in 0 ..< max(images.len - i * columns, 0):
let (_, icon) = images[i * columns + j]
let
(_, icon) = images[i * columns + j]
pos = vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32)
rendered.draw(
icon,
vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32),
translate(pos),
bmOverwrite
)

View file

@ -49,10 +49,12 @@ proc renderIconSet(index: int) =
for i in 0 ..< rows:
for j in 0 ..< max(images.len - i * columns, 0):
let (_, icon) = images[i * columns + j]
let
(_, icon) = images[i * columns + j]
pos = vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32)
rendered.draw(
icon,
vec2(((width + 4) * j + 2).float32, ((height + 4) * i + 2).float32),
translate(pos),
bmOverwrite
)

View file

@ -527,7 +527,7 @@ block:
image.fill(rgba(255, 255, 255, 255))
let paint = newPaint(pkSolid)
paint.color = rgba(0, 0, 255, 255)
paint.color = color(0, 0, 1, 1)
paint.blendMode = bmExclusion
ctx.fillStyle = paint
@ -638,9 +638,9 @@ block:
block:
let ctx = newContext(newImage(100, 100))
ctx.fillStyle.color = rgba(255, 0, 0, 255)
ctx.fillStyle.color = color(1, 0, 0, 1)
ctx.save()
ctx.fillStyle.color = rgba(0, 0, 255, 255)
ctx.fillStyle.color = color(0, 0, 1, 1)
ctx.restore()
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
ctx.image.writeFile("tests/images/context/paintSaveRestore.png")

View file

@ -640,8 +640,8 @@ block:
vec2(100, 50),
]
font.paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 127), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.5), position: 1.0),
]
let image = newImage(100, 100)
@ -765,7 +765,7 @@ block:
var font8 = newFont(ubuntu)
font8.size = 54
font8.paint.color = rgba(255, 0, 0, 255)
font8.paint.color = color(1, 0, 0, 1)
var font9 = newFont(roboto)
font9.size = 48

View file

@ -61,8 +61,8 @@ block:
vec2(100, 50),
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
let image = newImage(100, 100)
@ -77,8 +77,8 @@ block:
vec2(50, 100)
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
let image = newImage(100, 100)
@ -93,8 +93,8 @@ block:
vec2(50, 100)
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
let image = newImage(100, 100)
@ -109,8 +109,8 @@ block:
vec2(50, 100)
]
paint.gradientStops = @[
ColorStop(color: rgba(255, 0, 0, 255), position: 0),
ColorStop(color: rgba(255, 0, 0, 40), position: 1.0),
ColorStop(color: color(1, 0, 0, 1), position: 0),
ColorStop(color: color(1, 0, 0, 0.15625), position: 1.0),
]
paint.opacity = 0.5

View file

@ -371,7 +371,7 @@ block:
)
let paint = newPaint(pkSolid)
paint.color = rgbx(0, 255, 0, 255)
paint.color = color(0, 1, 0, 1)
paint.blendMode = bmExcludeMask
image.fillPath(
@ -388,7 +388,7 @@ block:
)
let paint = newPaint(pkSolid)
paint.color = rgbx(0, 255, 0, 255)
paint.color = color(0, 1, 0, 1)
paint.blendMode = bmExcludeMask
image.fillPath(
@ -405,7 +405,7 @@ block:
)
let paint = newPaint(pkSolid)
paint.color = rgbx(0, 255, 0, 255)
paint.color = color(0, 1, 0, 1)
paint.blendMode = bmMask
image.fillPath(
@ -422,7 +422,7 @@ block:
)
let paint = newPaint(pkSolid)
paint.color = rgbx(0, 255, 0, 255)
paint.color = color(0, 1, 0, 1)
paint.blendMode = bmMask
image.fillPath(
@ -594,7 +594,7 @@ block:
path.circle(50, 50, 30)
let paint = newPaint(pkSolid)
paint.color = rgba(255, 0, 255, 255)
paint.color = color(1, 0, 1, 1)
paint.opacity = 0.5
let image = newImage(100, 100)
@ -607,7 +607,7 @@ block:
path.circle(50, 50, 30)
let paint = newPaint(pkSolid)
paint.color = rgba(255, 0, 255, 255)
paint.color = color(1, 0, 1, 1)
paint.opacity = 0.5
let image = newImage(100, 100)