From 505e2ff2d97c8f8ebde3645d3a71309cd0b17d64 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Thu, 18 Feb 2021 13:29:40 -0600 Subject: [PATCH] drawRect image, mask --- src/pixie.nim | 21 +++++++++++++++++---- src/pixie/gradients.nim | 2 +- src/pixie/paths.nim | 3 +++ tests/images/drawRect.png | Bin 0 -> 355 bytes tests/images/masks/drawRect.png | Bin 0 -> 180 bytes tests/test_images_draw.nim | 8 +++++++- tests/test_masks.nim | 7 ++++++- 7 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/images/drawRect.png create mode 100644 tests/images/masks/drawRect.png diff --git a/src/pixie.nim b/src/pixie.nim index 384cfe0..faada81 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -1,8 +1,9 @@ -import flatty/binny, os, pixie/blends, pixie/common, pixie/fileformats/bmp, - pixie/fileformats/jpg, pixie/fileformats/png, pixie/fileformats/svg, - pixie/images, pixie/masks, pixie/paths, pixie/gradients +import bumpy, chroma, flatty/binny, os, pixie/blends, pixie/common, + pixie/fileformats/bmp, pixie/fileformats/jpg, pixie/fileformats/png, + pixie/fileformats/svg, pixie/gradients, pixie/images, pixie/masks, + pixie/paths, vmath -export blends, common, images, masks, paths, gradients +export blends, bumpy, chroma, common, gradients, images, masks, paths, vmath type FileFormat* = enum @@ -48,3 +49,15 @@ proc writeFile*(image: Image, filePath: string) = else: raise newException(PixieError, "Unsupported image file extension") image.writeFile(filePath, fileformat) + +proc drawRect*( + image: Image, rect: Rect, color: ColorRGBA, blendMode = bmNormal +) = + var path: Path + path.rect(rect) + image.fillPath(path, color, wrNonZero, blendMode) + +proc drawRect*(mask: Mask, rect: Rect) = + var path: Path + path.rect(rect) + mask.fillPath(path) diff --git a/src/pixie/gradients.nim b/src/pixie/gradients.nim index e60989d..95b23f5 100644 --- a/src/pixie/gradients.nim +++ b/src/pixie/gradients.nim @@ -1,4 +1,4 @@ -import images, vmath, chroma, common +import chroma, common, images, vmath type ColorStop* = object ## Represents color on a gradient curve. diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index 76ad11d..acd5308 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -370,6 +370,9 @@ proc rect*(path: var Path, x, y, w, h: float32, clockwise = true) = proc rect*(path: var Path, pos: Vec2, wh: Vec2, clockwise = true) {.inline.} = path.rect(pos.x, pos.y, wh.x, wh.y, clockwise) +proc rect*(path: var Path, rect: Rect, clockwise = true) {.inline.} = + path.rect(rect.x, rect.y, rect.w, rect.h, clockwise) + const splineCircleK = 4.0 * (-1.0 + sqrt(2.0)) / 3 proc roundedRect*( diff --git a/tests/images/drawRect.png b/tests/images/drawRect.png new file mode 100644 index 0000000000000000000000000000000000000000..e65117059041749effba8dcdf360c20a5d9adb61 GIT binary patch literal 355 zcmeAS@N?(olHy`uVBq!ia0vp^DIm#%_UgClEw zOy$m|%j!EOiQ6!T%Vz|ZACJiHwOpRL=io_Yop!w8OqK~m*!yJt#=C~k m=G@-?X)??&a8F@n$Ok7aS)ZUgeJ(KY7(8A5T-G@yGywp3?Q+Zj literal 0 HcmV?d00001 diff --git a/tests/images/masks/drawRect.png b/tests/images/masks/drawRect.png new file mode 100644 index 0000000000000000000000000000000000000000..9afe3ccd27ddaea17b246f7281371ca9b082beb8 GIT binary patch literal 180 zcmeAS@N?(olHy`uVBq!ia0vp^DIm-NBp5dt*RC$gpl^cD+k&UR%uj h%$6zLgkjC3*DM|nY%>lf=CgvFvd$@?2>@S_NP_?X literal 0 HcmV?d00001 diff --git a/tests/test_images_draw.nim b/tests/test_images_draw.nim index 5e3f46f..3b3e945 100644 --- a/tests/test_images_draw.nim +++ b/tests/test_images_draw.nim @@ -1,4 +1,4 @@ -import chroma, pixie, vmath +import pixie block: let @@ -49,3 +49,9 @@ block: a.draw(b, translate(vec2(250, 250)) * rotationMat3(360 * PI / 180)) a.writeFile("tests/images/rotate360.png") + +block: + let image = newImage(100, 100) + image.fill(rgba(0, 255, 255, 255)) + image.drawRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255)) + image.writeFile("tests/images/drawRect.png") diff --git a/tests/test_masks.nim b/tests/test_masks.nim index e5b2647..b6171f8 100644 --- a/tests/test_masks.nim +++ b/tests/test_masks.nim @@ -1,4 +1,4 @@ -import chroma, pixie, pixie/fileformats/png, vmath +import pixie, pixie/fileformats/png block: let mask = newMask(100, 100) @@ -102,3 +102,8 @@ block: mask.ceil() writeFile("tests/images/masks/circleMaskSharpened.png", mask.encodePng()) + +block: + let mask = newMask(100, 100) + mask.drawRect(rect(vec2(10, 10), vec2(30, 30))) + writeFile("tests/images/masks/drawRect.png", mask.encodePng())