Rename PaintKind enum.

This commit is contained in:
treeform 2022-02-12 10:25:28 -08:00
parent 95886c4861
commit 5074e33efc
15 changed files with 56 additions and 56 deletions

View file

@ -235,7 +235,7 @@ image.draw(lines)
### Gradient ### Gradient
nim c -r [examples/gradient.nim](examples/gradient.nim) nim c -r [examples/gradient.nim](examples/gradient.nim)
```nim ```nim
let paint = newPaint(pkGradientRadial) let paint = newPaint(PaintGradientRadial)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(100, 100), vec2(100, 100),
vec2(200, 100), vec2(200, 100),
@ -270,7 +270,7 @@ path.polygon(
sides = 8 sides = 8
) )
let paint = newPaint(pkImageTiled) let paint = newPaint(PaintImageTiled)
paint.image = readImage("examples/data/mandrill.png") paint.image = readImage("examples/data/mandrill.png")
paint.imageMat = scale(vec2(0.08, 0.08)) paint.imageMat = scale(vec2(0.08, 0.08))

View file

@ -3,7 +3,7 @@ import pixie
let image = newImage(200, 200) let image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255)) image.fill(rgba(255, 255, 255, 255))
let paint = newPaint(pkGradientRadial) let paint = newPaint(PaintGradientRadial)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(100, 100), vec2(100, 100),
vec2(200, 100), vec2(200, 100),

View file

@ -10,7 +10,7 @@ path.polygon(
sides = 8 sides = 8
) )
let paint = newPaint(pkImageTiled) let paint = newPaint(PaintImageTiled)
paint.image = readImage("examples/data/mandrill.png") paint.image = readImage("examples/data/mandrill.png")
paint.imageMat = scale(vec2(0.08, 0.08)) paint.imageMat = scale(vec2(0.08, 0.08))

View file

@ -358,7 +358,7 @@ block:
# doDiff(readImage("cairo4.png"), a, "4") # doDiff(readImage("cairo4.png"), a, "4")
var b: Image var b: Image
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(1, 0, 0, 0.5) paint.color = color(1, 0, 0, 0.5)
paint.blendMode = BlendOverwrite paint.blendMode = BlendOverwrite

View file

@ -192,7 +192,7 @@ proc decodeCtxInternal(inherited: Ctx, node: XmlNode): Ctx =
let id = fill[5 .. ^2] let id = fill[5 .. ^2]
if id in result.linearGradients: if id in result.linearGradients:
let linearGradient = result.linearGradients[id] let linearGradient = result.linearGradients[id]
result.fill = newPaint(pkGradientLinear) result.fill = newPaint(PaintGradientLinear)
result.fill.gradientHandlePositions = @[ result.fill.gradientHandlePositions = @[
result.transform * vec2(linearGradient.x1, linearGradient.y1), result.transform * vec2(linearGradient.x1, linearGradient.y1),
result.transform * vec2(linearGradient.x2, linearGradient.y2) result.transform * vec2(linearGradient.x2, linearGradient.y2)

View file

@ -53,9 +53,9 @@ proc newContext*(image: Image): Context {.raises: [].} =
result.globalAlpha = 1 result.globalAlpha = 1
result.lineWidth = 1 result.lineWidth = 1
result.miterLimit = 10 result.miterLimit = 10
result.fillStyle = newPaint(pkSolid) result.fillStyle = newPaint(PaintSolid)
result.fillStyle.color = color(0, 0, 0, 1) result.fillStyle.color = color(0, 0, 0, 1)
result.strokeStyle = newPaint(pkSolid) result.strokeStyle = newPaint(PaintSolid)
result.strokeStyle.color = color(0, 0, 0, 1) result.strokeStyle.color = color(0, 0, 0, 1)
result.fontSize = 12 result.fontSize = 12
@ -397,7 +397,7 @@ proc stroke*(ctx: Context) {.inline, raises: [PixieError].} =
proc clearRect*(ctx: Context, rect: Rect) {.raises: [PixieError].} = proc clearRect*(ctx: Context, rect: Rect) {.raises: [PixieError].} =
## Erases the pixels in a rectangular area. ## Erases the pixels in a rectangular area.
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.blendMode = BlendOverwrite paint.blendMode = BlendOverwrite
let path = newPath() let path = newPath()
@ -541,7 +541,7 @@ proc drawImage*(
)) ))
savedFillStyle = ctx.fillStyle savedFillStyle = ctx.fillStyle
ctx.fillStyle = newPaint(pkImage) ctx.fillStyle = newPaint(PaintImage)
ctx.fillStyle.image = image ctx.fillStyle.image = image
ctx.fillStyle.imageMat = imageMat ctx.fillStyle.imageMat = imageMat

View file

@ -169,7 +169,7 @@ proc decodeCtxInternal(inherited: Ctx, node: XmlNode): Ctx =
let id = fill[5 .. ^2] let id = fill[5 .. ^2]
if id in result.linearGradients: if id in result.linearGradients:
let linearGradient = result.linearGradients[id] let linearGradient = result.linearGradients[id]
result.fill = newPaint(pkGradientLinear) result.fill = newPaint(PaintGradientLinear)
result.fill.gradientHandlePositions = @[ result.fill.gradientHandlePositions = @[
result.transform * vec2(linearGradient.x1, linearGradient.y1), result.transform * vec2(linearGradient.x1, linearGradient.y1),
result.transform * vec2(linearGradient.x2, linearGradient.y2) result.transform * vec2(linearGradient.x2, linearGradient.y2)

View file

@ -197,7 +197,7 @@ proc newFont*(typeface: Typeface): Font {.raises: [].} =
result.typeface = typeface result.typeface = typeface
result.size = 12 result.size = 12
result.lineHeight = autoLineHeight result.lineHeight = autoLineHeight
result.paint = newPaint(pkSolid) result.paint = newPaint(PaintSolid)
result.paint.color = color(0, 0, 0, 1) result.paint.color = color(0, 0, 0, 1)
proc newSpan*(text: string, font: Font): Span {.raises: [].} = proc newSpan*(text: string, font: Font): Span {.raises: [].} =

View file

@ -5,24 +5,24 @@ when defined(amd64) and not defined(pixieNoSimd):
type type
PaintKind* = enum PaintKind* = enum
pkSolid PaintSolid
pkImage PaintImage
pkImageTiled PaintImageTiled
pkGradientLinear PaintGradientLinear
pkGradientRadial PaintGradientRadial
pkGradientAngular PaintGradientAngular
Paint* = ref object Paint* = ref object
## Paint used to fill paths. ## Paint used to fill paths.
kind*: PaintKind kind*: PaintKind
blendMode*: BlendMode ## Blend mode. blendMode*: BlendMode ## Blend mode.
opacity*: float32 opacity*: float32
# pkSolid # PaintSolid
color*: Color ## Color to fill with. color*: Color ## Color to fill with.
# pkImage, pkImageTiled: # PaintImage, PaintImageTiled:
image*: Image ## Image to fill with. image*: Image ## Image to fill with.
imageMat*: Mat3 ## Matrix of the filled image. imageMat*: Mat3 ## Matrix of the filled image.
# pkGradientLinear, pkGradientRadial, pkGradientAngular: # PaintGradientLinear, PaintGradientRadial, PaintGradientAngular:
gradientHandlePositions*: seq[Vec2] ## Gradient positions (image space). gradientHandlePositions*: seq[Vec2] ## Gradient positions (image space).
gradientStops*: seq[ColorStop] ## Color stops (gradient space). gradientStops*: seq[ColorStop] ## Color stops (gradient space).
@ -51,13 +51,13 @@ proc newPaint*(paint: Paint): Paint {.raises: [].} =
converter parseSomePaint*(paint: SomePaint): Paint {.inline.} = converter parseSomePaint*(paint: SomePaint): Paint {.inline.} =
## Given SomePaint, parse it in different ways. ## Given SomePaint, parse it in different ways.
when type(paint) is string: when type(paint) is string:
result = newPaint(pkSolid) result = newPaint(PaintSolid)
try: try:
result.color = parseHtmlColor(paint) result.color = parseHtmlColor(paint)
except: except:
raise newException(PixieError, "Unable to parse color " & paint) raise newException(PixieError, "Unable to parse color " & paint)
elif type(paint) is SomeColor: elif type(paint) is SomeColor:
result = newPaint(pkSolid) result = newPaint(PaintSolid)
when type(paint) is Color: when type(paint) is Color:
result.color = paint result.color = paint
else: else:
@ -229,11 +229,11 @@ proc fillGradient*(image: Image, paint: Paint) {.raises: [PixieError].} =
## Fills with the Paint gradient. ## Fills with the Paint gradient.
case paint.kind: case paint.kind:
of pkGradientLinear: of PaintGradientLinear:
image.fillGradientLinear(paint) image.fillGradientLinear(paint)
of pkGradientRadial: of PaintGradientRadial:
image.fillGradientRadial(paint) image.fillGradientRadial(paint)
of pkGradientAngular: of PaintGradientAngular:
image.fillGradientAngular(paint) image.fillGradientAngular(paint)
else: else:
raise newException(PixieError, "Paint must be a gradient") raise newException(PixieError, "Paint must be a gradient")

View file

@ -1901,7 +1901,7 @@ proc fillPath*(
if paint.opacity == 0: if paint.opacity == 0:
return return
if paint.kind == pkSolid: if paint.kind == PaintSolid:
if paint.color.a > 0 or paint.blendMode == BlendOverwrite: if paint.color.a > 0 or paint.blendMode == BlendOverwrite:
var shapes = parseSomePath(path, true, transform.pixelScale()) var shapes = parseSomePath(path, true, transform.pixelScale())
shapes.transform(transform) shapes.transform(transform)
@ -1922,13 +1922,13 @@ proc fillPath*(
paint.opacity = 1 paint.opacity = 1
case paint.kind: case paint.kind:
of pkSolid: of PaintSolid:
discard # Handled above discard # Handled above
of pkImage: of PaintImage:
fill.draw(paint.image, paint.imageMat) fill.draw(paint.image, paint.imageMat)
of pkImageTiled: of PaintImageTiled:
fill.drawTiled(paint.image, paint.imageMat) fill.drawTiled(paint.image, paint.imageMat)
of pkGradientLinear, pkGradientRadial, pkGradientAngular: of PaintGradientLinear, PaintGradientRadial, PaintGradientAngular:
fill.fillGradient(paint) fill.fillGradient(paint)
paint.opacity = savedOpacity paint.opacity = savedOpacity
@ -1979,7 +1979,7 @@ proc strokePath*(
if paint.opacity == 0: if paint.opacity == 0:
return return
if paint.kind == pkSolid: if paint.kind == PaintSolid:
if paint.color.a > 0 or paint.blendMode == BlendOverwrite: if paint.color.a > 0 or paint.blendMode == BlendOverwrite:
var strokeShapes = strokeShapes( var strokeShapes = strokeShapes(
parseSomePath(path, false, transform.pixelScale()), parseSomePath(path, false, transform.pixelScale()),
@ -2016,13 +2016,13 @@ proc strokePath*(
paint.opacity = 1 paint.opacity = 1
case paint.kind: case paint.kind:
of pkSolid: of PaintSolid:
discard # Handled above discard # Handled above
of pkImage: of PaintImage:
fill.draw(paint.image, paint.imageMat) fill.draw(paint.image, paint.imageMat)
of pkImageTiled: of PaintImageTiled:
fill.drawTiled(paint.image, paint.imageMat) fill.drawTiled(paint.image, paint.imageMat)
of pkGradientLinear, pkGradientRadial, pkGradientAngular: of PaintGradientLinear, PaintGradientRadial, PaintGradientAngular:
fill.fillGradient(paint) fill.fillGradient(paint)
paint.opacity = savedOpacity paint.opacity = savedOpacity

View file

@ -3,7 +3,7 @@ import benchy, pixie
let image = newImage(1000, 1000) let image = newImage(1000, 1000)
timeIt "GradientLinear vertical": timeIt "GradientLinear vertical":
let paint = newPaint(pkGradientLinear) let paint = newPaint(PaintGradientLinear)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(50, 0), vec2(50, 0),
vec2(50, 1000), vec2(50, 1000),
@ -15,7 +15,7 @@ timeIt "GradientLinear vertical":
image.fillGradient(paint) image.fillGradient(paint)
timeIt "GradientLinear horizontal": timeIt "GradientLinear horizontal":
let paint = newPaint(pkGradientLinear) let paint = newPaint(PaintGradientLinear)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(0, 50), vec2(0, 50),
vec2(1000, 50), vec2(1000, 50),
@ -30,7 +30,7 @@ timeIt "GradientLinear horizontal":
# discard # discard
timeIt "GradientLinear angular": timeIt "GradientLinear angular":
let paint = newPaint(pkGradientAngular) let paint = newPaint(PaintGradientAngular)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(500, 500), vec2(500, 500),
vec2(1000, 500), vec2(1000, 500),

View file

@ -526,7 +526,7 @@ block:
image.fill(rgba(255, 255, 255, 255)) image.fill(rgba(255, 255, 255, 255))
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(0, 0, 1, 1) paint.color = color(0, 0, 1, 1)
paint.blendMode = BlendExclusion paint.blendMode = BlendExclusion

View file

@ -661,7 +661,7 @@ block:
block: block:
var font = readFont("tests/fonts/IBMPlexSans-Regular_2.ttf") var font = readFont("tests/fonts/IBMPlexSans-Regular_2.ttf")
font.size = 48 font.size = 48
font.paint = newPaint(pkGradientLinear) font.paint = newPaint(PaintGradientLinear)
font.paint.gradientHandlePositions = @[ font.paint.gradientHandlePositions = @[
vec2(0, 50), vec2(0, 50),
vec2(100, 50), vec2(100, 50),

View file

@ -17,7 +17,7 @@ block:
image.writeFile("tests/paths/paintSolid.png") image.writeFile("tests/paths/paintSolid.png")
block: block:
let paint = newPaint(pkImage) let paint = newPaint(PaintImage)
paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png"))
paint.imageMat = scale(vec2(0.2, 0.2)) paint.imageMat = scale(vec2(0.2, 0.2))
@ -26,7 +26,7 @@ block:
image.writeFile("tests/paths/paintImage.png") image.writeFile("tests/paths/paintImage.png")
block: block:
let paint = newPaint(pkImage) let paint = newPaint(PaintImage)
paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png"))
paint.imageMat = scale(vec2(0.2, 0.2)) paint.imageMat = scale(vec2(0.2, 0.2))
paint.opacity = 0.5 paint.opacity = 0.5
@ -36,7 +36,7 @@ block:
image.writeFile("tests/paths/paintImageOpacity.png") image.writeFile("tests/paths/paintImageOpacity.png")
block: block:
let paint = newPaint(pkImageTiled) let paint = newPaint(PaintImageTiled)
paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png"))
paint.imageMat = scale(vec2(0.02, 0.02)) paint.imageMat = scale(vec2(0.02, 0.02))
@ -45,7 +45,7 @@ block:
image.writeFile("tests/paths/paintImageTiled.png") image.writeFile("tests/paths/paintImageTiled.png")
block: block:
let paint = newPaint(pkImageTiled) let paint = newPaint(PaintImageTiled)
paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png"))
paint.imageMat = scale(vec2(0.02, 0.02)) paint.imageMat = scale(vec2(0.02, 0.02))
paint.opacity = 0.5 paint.opacity = 0.5
@ -55,7 +55,7 @@ block:
image.writeFile("tests/paths/paintImageTiledOpacity.png") image.writeFile("tests/paths/paintImageTiledOpacity.png")
block: block:
let paint = newPaint(pkGradientLinear) let paint = newPaint(PaintGradientLinear)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(0, 50), vec2(0, 50),
vec2(100, 50), vec2(100, 50),
@ -70,7 +70,7 @@ block:
image.writeFile("tests/paths/gradientLinear.png") image.writeFile("tests/paths/gradientLinear.png")
block: block:
let paint = newPaint(pkGradientLinear) let paint = newPaint(PaintGradientLinear)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(50, 0), vec2(50, 0),
vec2(50, 100), vec2(50, 100),
@ -85,7 +85,7 @@ block:
image.writeFile("tests/paths/gradientLinear2.png") image.writeFile("tests/paths/gradientLinear2.png")
block: block:
let paint = newPaint(pkGradientRadial) let paint = newPaint(PaintGradientRadial)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(50, 50), vec2(50, 50),
vec2(100, 50), vec2(100, 50),
@ -101,7 +101,7 @@ block:
image.writeFile("tests/paths/gradientRadial.png") image.writeFile("tests/paths/gradientRadial.png")
block: block:
let paint = newPaint(pkGradientAngular) let paint = newPaint(PaintGradientAngular)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(50, 50), vec2(50, 50),
vec2(100, 50), vec2(100, 50),
@ -117,7 +117,7 @@ block:
image.writeFile("tests/paths/gradientAngular.png") image.writeFile("tests/paths/gradientAngular.png")
block: block:
let paint = newPaint(pkGradientAngular) let paint = newPaint(PaintGradientAngular)
paint.gradientHandlePositions = @[ paint.gradientHandlePositions = @[
vec2(50, 50), vec2(50, 50),
vec2(100, 50), vec2(100, 50),

View file

@ -390,7 +390,7 @@ block:
rgbx(255, 0, 0, 255) rgbx(255, 0, 0, 255)
) )
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(0, 1, 0, 1) paint.color = color(0, 1, 0, 1)
paint.blendMode = BlendExcludeMask paint.blendMode = BlendExcludeMask
@ -407,7 +407,7 @@ block:
rgbx(255, 0, 0, 255) rgbx(255, 0, 0, 255)
) )
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(0, 1, 0, 1) paint.color = color(0, 1, 0, 1)
paint.blendMode = BlendExcludeMask paint.blendMode = BlendExcludeMask
@ -424,7 +424,7 @@ block:
rgbx(255, 0, 0, 255) rgbx(255, 0, 0, 255)
) )
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(0, 1, 0, 1) paint.color = color(0, 1, 0, 1)
paint.blendMode = BlendMask paint.blendMode = BlendMask
@ -441,7 +441,7 @@ block:
rgbx(255, 0, 0, 255) rgbx(255, 0, 0, 255)
) )
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(0, 1, 0, 1) paint.color = color(0, 1, 0, 1)
paint.blendMode = BlendMask paint.blendMode = BlendMask
@ -613,7 +613,7 @@ block:
let path = newPath() let path = newPath()
path.circle(50, 50, 30) path.circle(50, 50, 30)
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(1, 0, 1, 1) paint.color = color(1, 0, 1, 1)
paint.opacity = 0.5 paint.opacity = 0.5
@ -626,7 +626,7 @@ block:
let path = newPath() let path = newPath()
path.circle(50, 50, 30) path.circle(50, 50, 30)
let paint = newPaint(pkSolid) let paint = newPaint(PaintSolid)
paint.color = color(1, 0, 1, 1) paint.color = color(1, 0, 1, 1)
paint.opacity = 0.5 paint.opacity = 0.5