v2.0.0 add transform to pixie.nim helpers

This commit is contained in:
Ryan Oldenburg 2021-05-17 23:36:34 -05:00
parent fcc947ea60
commit d87b423430
4 changed files with 118 additions and 51 deletions

View file

@ -1,4 +1,4 @@
version = "1.2.0" version = "2.0.0"
author = "Andre von Houck and Ryan Oldenburg" author = "Andre von Houck and Ryan Oldenburg"
description = "Full-featured 2d graphics library for Nim." description = "Full-featured 2d graphics library for Nim."
license = "MIT" license = "MIT"

View file

@ -77,231 +77,292 @@ proc writeFile*(image: Image, filePath: string) =
raise newException(PixieError, "Unsupported image file extension") raise newException(PixieError, "Unsupported image file extension")
image.writeFile(filePath, fileformat) image.writeFile(filePath, fileformat)
proc fillRect*(image: Image, rect: Rect, color: SomeColor) = proc fillRect*(
image: Image,
rect: Rect,
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
## Fills a rectangle. ## Fills a rectangle.
var path: Path var path: Path
path.rect(rect) path.rect(rect)
image.fillPath(path, color) image.fillPath(path, color, transform)
proc fillRect*(mask: Mask, rect: Rect) = proc fillRect*(
mask: Mask,
rect: Rect,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
## Fills a rectangle. ## Fills a rectangle.
var path: Path var path: Path
path.rect(rect) path.rect(rect)
mask.fillPath(path) mask.fillPath(path, transform)
proc strokeRect*( proc strokeRect*(
image: Image, rect: Rect, color: SomeColor, strokeWidth = 1.0 image: Image,
rect: Rect,
color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) = ) =
## Strokes a rectangle. ## Strokes a rectangle.
var path: Path var path: Path
path.rect(rect) path.rect(rect)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeRect*(mask: Mask, rect: Rect, strokeWidth = 1.0) = proc strokeRect*(
mask: Mask,
rect: Rect,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) =
## Strokes a rectangle. ## Strokes a rectangle.
var path: Path var path: Path
path.rect(rect) path.rect(rect)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc fillRoundedRect*( proc fillRoundedRect*(
image: Image, image: Image,
rect: Rect, rect: Rect,
nw, ne, se, sw: float32, nw, ne, se, sw: float32,
color: SomeColor color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills a rounded rectangle. ## Fills a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, nw, ne, se, sw) path.roundedRect(rect, nw, ne, se, sw)
image.fillPath(path, color) image.fillPath(path, color, transform)
proc fillRoundedRect*( proc fillRoundedRect*(
image: Image, image: Image,
rect: Rect, rect: Rect,
radius: float32, radius: float32,
color: SomeColor color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills a rounded rectangle. ## Fills a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, radius, radius, radius, radius) path.roundedRect(rect, radius, radius, radius, radius)
image.fillPath(path, color) image.fillPath(path, color, transform)
proc fillRoundedRect*(mask: Mask, rect: Rect, nw, ne, se, sw: float32) = proc fillRoundedRect*(
mask: Mask,
rect: Rect,
nw, ne, se, sw: float32,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
## Fills a rounded rectangle. ## Fills a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, nw, ne, se, sw) path.roundedRect(rect, nw, ne, se, sw)
mask.fillPath(path) mask.fillPath(path, transform)
proc fillRoundedRect*(mask: Mask, rect: Rect, radius: float32) = proc fillRoundedRect*(
mask: Mask,
rect: Rect,
radius: float32,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
## Fills a rounded rectangle. ## Fills a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, radius, radius, radius, radius) path.roundedRect(rect, radius, radius, radius, radius)
mask.fillPath(path) mask.fillPath(path, transform)
proc strokeRoundedRect*( proc strokeRoundedRect*(
image: Image, image: Image,
rect: Rect, rect: Rect,
nw, ne, se, sw: float32, nw, ne, se, sw: float32,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a rounded rectangle. ## Strokes a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, nw, ne, se, sw) path.roundedRect(rect, nw, ne, se, sw)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeRoundedRect*( proc strokeRoundedRect*(
image: Image, image: Image,
rect: Rect, rect: Rect,
radius: float32, radius: float32,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a rounded rectangle. ## Strokes a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, radius, radius, radius, radius) path.roundedRect(rect, radius, radius, radius, radius)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeRoundedRect*( proc strokeRoundedRect*(
mask: Mask, rect: Rect, nw, ne, se, sw: float32, strokeWidth = 1.0 mask: Mask,
rect: Rect,
nw, ne, se, sw: float32,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) = ) =
## Strokes a rounded rectangle. ## Strokes a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, nw, ne, se, sw) path.roundedRect(rect, nw, ne, se, sw)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc strokeRoundedRect*( proc strokeRoundedRect*(
mask: Mask, rect: Rect, radius: float32, strokeWidth = 1.0 mask: Mask,
rect: Rect,
radius: float32,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) = ) =
## Strokes a rounded rectangle. ## Strokes a rounded rectangle.
var path: Path var path: Path
path.roundedRect(rect, radius, radius, radius, radius) path.roundedRect(rect, radius, radius, radius, radius)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc strokeSegment*( proc strokeSegment*(
image: Image, image: Image,
segment: Segment, segment: Segment,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a segment (draws a line from segment.at to segment.to). ## Strokes a segment (draws a line from segment.at to segment.to).
var path: Path var path: Path
path.moveTo(segment.at) path.moveTo(segment.at)
path.lineTo(segment.to) path.lineTo(segment.to)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeSegment*(mask: Mask, segment: Segment, strokeWidth: float32) = proc strokeSegment*(
mask: Mask,
segment: Segment,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0
) =
## Strokes a segment (draws a line from segment.at to segment.to). ## Strokes a segment (draws a line from segment.at to segment.to).
var path: Path var path: Path
path.moveTo(segment.at) path.moveTo(segment.at)
path.lineTo(segment.to) path.lineTo(segment.to)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc fillEllipse*( proc fillEllipse*(
image: Image, image: Image,
center: Vec2, center: Vec2,
rx, ry: float32, rx, ry: float32,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
blendMode = bmNormal blendMode = bmNormal
) = ) =
## Fills an ellipse. ## Fills an ellipse.
var path: Path var path: Path
path.ellipse(center, rx, ry) path.ellipse(center, rx, ry)
image.fillPath(path, color, wrNonZero, blendMode) image.fillPath(path, color, transform, wrNonZero, blendMode)
proc fillEllipse*( proc fillEllipse*(
mask: Mask, mask: Mask,
center: Vec2, center: Vec2,
rx, ry: float32 rx, ry: float32,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills an ellipse. ## Fills an ellipse.
var path: Path var path: Path
path.ellipse(center, rx, ry) path.ellipse(center, rx, ry)
mask.fillPath(path) mask.fillPath(path, transform)
proc strokeEllipse*( proc strokeEllipse*(
image: Image, image: Image,
center: Vec2, center: Vec2,
rx, ry: float32, rx, ry: float32,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes an ellipse. ## Strokes an ellipse.
var path: Path var path: Path
path.ellipse(center, rx, ry) path.ellipse(center, rx, ry)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeEllipse*( proc strokeEllipse*(
mask: Mask, mask: Mask,
center: Vec2, center: Vec2,
rx, ry: float32, rx, ry: float32,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes an ellipse. ## Strokes an ellipse.
var path: Path var path: Path
path.ellipse(center, rx, ry) path.ellipse(center, rx, ry)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc fillCircle*( proc fillCircle*(
image: Image, image: Image,
center: Vec2, center: Vec2,
radius: float32, radius: float32,
color: SomeColor color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills a circle. ## Fills a circle.
var path: Path var path: Path
path.ellipse(center, radius, radius) path.ellipse(center, radius, radius)
image.fillPath(path, color) image.fillPath(path, color, transform)
proc fillCircle*( proc fillCircle*(
mask: Mask, mask: Mask,
center: Vec2, center: Vec2,
radius: float32 radius: float32,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills a circle. ## Fills a circle.
var path: Path var path: Path
path.ellipse(center, radius, radius) path.ellipse(center, radius, radius)
mask.fillPath(path) mask.fillPath(path, transform)
proc strokeCircle*( proc strokeCircle*(
image: Image, image: Image,
center: Vec2, center: Vec2,
radius: float32, radius: float32,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a circle. ## Strokes a circle.
var path: Path var path: Path
path.ellipse(center, radius, radius) path.ellipse(center, radius, radius)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokeCircle*( proc strokeCircle*(
mask: Mask, mask: Mask,
center: Vec2, center: Vec2,
radius: float32, radius: float32,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a circle. ## Strokes a circle.
var path: Path var path: Path
path.ellipse(center, radius, radius) path.ellipse(center, radius, radius)
mask.fillPath(path) mask.fillPath(path, transform, strokeWidth)
proc fillPolygon*( proc fillPolygon*(
image: Image, image: Image,
pos: Vec2, pos: Vec2,
size: float32, size: float32,
sides: int, sides: int,
color: SomeColor color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0)
) = ) =
## Fills a polygon. ## Fills a polygon.
var path: Path var path: Path
path.polygon(pos, size, sides) path.polygon(pos, size, sides)
image.fillPath(path, color) image.fillPath(path, color, transform)
proc fillPolygon*(mask: Mask, pos: Vec2, size: float32, sides: int) = proc fillPolygon*(
mask: Mask,
pos: Vec2,
size: float32,
sides: int,
transform: Vec2 | Mat3 = vec2(0, 0)
) =
## Fills a polygon. ## Fills a polygon.
var path: Path var path: Path
path.polygon(pos, size, sides) path.polygon(pos, size, sides)
mask.fillPath(path) mask.fillPath(path, transform)
proc strokePolygon*( proc strokePolygon*(
image: Image, image: Image,
@ -309,24 +370,26 @@ proc strokePolygon*(
size: float32, size: float32,
sides: int, sides: int,
color: SomeColor, color: SomeColor,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a polygon. ## Strokes a polygon.
var path: Path var path: Path
path.polygon(pos, size, sides) path.polygon(pos, size, sides)
image.strokePath(path, color, strokeWidth) image.strokePath(path, color, transform, strokeWidth)
proc strokePolygon*( proc strokePolygon*(
mask: Mask, mask: Mask,
pos: Vec2, pos: Vec2,
size: float32, size: float32,
sides: int, sides: int,
transform: Vec2 | Mat3 = vec2(0, 0),
strokeWidth = 1.0 strokeWidth = 1.0
) = ) =
## Strokes a polygon. ## Strokes a polygon.
var path: Path var path: Path
path.polygon(pos, size, sides) path.polygon(pos, size, sides)
mask.strokePath(path, strokeWidth) mask.strokePath(path, transform, strokeWidth)
proc fillText*( proc fillText*(
target: Image | Mask, target: Image | Mask,

View file

@ -59,7 +59,11 @@ block:
block: block:
let image = newImage(100, 100) let image = newImage(100, 100)
image.fill(rgba(0, 255, 255, 255)) image.fill(rgba(0, 255, 255, 255))
image.strokeRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255), 10) image.strokeRect(
rect(vec2(10, 10), vec2(30, 30)),
rgba(255, 255, 0, 255),
strokeWidth = 10
)
image.writeFile("tests/images/strokeRect.png") image.writeFile("tests/images/strokeRect.png")
block: block:
@ -79,7 +83,7 @@ block:
rect(vec2(10, 10), vec2(30, 30)), rect(vec2(10, 10), vec2(30, 30)),
10, 10,
rgba(255, 255, 0, 255), rgba(255, 255, 0, 255),
10 strokeWidth = 10
) )
image.writeFile("tests/images/strokeRoundedRect.png") image.writeFile("tests/images/strokeRoundedRect.png")
@ -112,7 +116,7 @@ block:
25, 25,
25, 25,
rgba(255, 255, 0, 255), rgba(255, 255, 0, 255),
10 strokeWidth = 10
) )
image.writeFile("tests/images/strokeEllipse.png") image.writeFile("tests/images/strokeEllipse.png")
@ -135,6 +139,6 @@ block:
30, 30,
6, 6,
rgba(255, 255, 0, 255), rgba(255, 255, 0, 255),
10 strokeWidth = 10
) )
image.writeFile("tests/images/strokePolygon.png") image.writeFile("tests/images/strokePolygon.png")

View file

@ -109,7 +109,7 @@ block:
block: block:
let mask = newMask(100, 100) let mask = newMask(100, 100)
mask.strokeRect(rect(vec2(10, 10), vec2(30, 30)), 10) mask.strokeRect(rect(vec2(10, 10), vec2(30, 30)), strokeWidth = 10)
writeFile("tests/images/masks/strokeRect.png", mask.encodePng()) writeFile("tests/images/masks/strokeRect.png", mask.encodePng())
block: block:
@ -119,7 +119,7 @@ block:
block: block:
let mask = newMask(100, 100) let mask = newMask(100, 100)
mask.strokeRoundedRect(rect(vec2(10, 10), vec2(30, 30)), 10, 10) mask.strokeRoundedRect(rect(vec2(10, 10), vec2(30, 30)), 10, strokeWidth = 10)
writeFile("tests/images/masks/strokeRoundedRect.png", mask.encodePng()) writeFile("tests/images/masks/strokeRoundedRect.png", mask.encodePng())
block: block:
@ -137,7 +137,7 @@ block:
block: block:
let mask = newMask(100, 100) let mask = newMask(100, 100)
mask.strokeEllipse(vec2(50, 50), 20, 10, 10) mask.strokeEllipse(vec2(50, 50), 20, 10, strokeWidth = 10)
writeFile("tests/images/masks/strokeEllipse.png", mask.encodePng()) writeFile("tests/images/masks/strokeEllipse.png", mask.encodePng())
block: block:
@ -147,7 +147,7 @@ block:
block: block:
let mask = newMask(100, 100) let mask = newMask(100, 100)
mask.strokePolygon(vec2(50, 50), 30, 6, 10) mask.strokePolygon(vec2(50, 50), 30, 6, strokeWidth = 10)
writeFile("tests/images/masks/strokePolygon.png", mask.encodePng()) writeFile("tests/images/masks/strokePolygon.png", mask.encodePng())
block: block: