From 3658ff40c3818012200d31fcfb562d7aa657a5ed Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Mon, 31 May 2021 21:05:27 -0500 Subject: [PATCH] context globalAlpha --- src/pixie/context.nim | 51 +++++++++++++++++++++++-- tests/images/context/globalAlpha_1.png | Bin 0 -> 1079 bytes tests/test_context.nim | 17 +++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 tests/images/context/globalAlpha_1.png diff --git a/src/pixie/context.nim b/src/pixie/context.nim index 1f46bab..85c16a7 100644 --- a/src/pixie/context.nim +++ b/src/pixie/context.nim @@ -10,6 +10,7 @@ type Context* = ref object image*: Image fillStyle*, strokeStyle*: Paint + globalAlpha*: float32 lineWidth*: float32 miterLimit*: float32 lineCap*: LineCap @@ -25,6 +26,7 @@ type ContextState = object fillStyle, strokeStyle: Paint + globalAlpha: float32 lineWidth: float32 miterLimit: float32 lineCap: LineCap @@ -44,6 +46,7 @@ proc newContext*(image: Image): Context = result = Context() result.image = image result.mat = mat3() + result.globalAlpha = 1 result.lineWidth = 1 result.miterLimit = 10 result.fillStyle = Paint(kind: pkSolid, color: rgbx(0, 0, 0, 255)) @@ -56,6 +59,7 @@ proc newContext*(width, height: int): Context {.inline.} = proc state(ctx: Context): ContextState = result.fillStyle = ctx.fillStyle result.strokeStyle = ctx.strokeStyle + result.globalAlpha = ctx.globalAlpha result.lineWidth = ctx.lineWidth result.miterLimit = ctx.miterLimit result.lineCap = ctx.lineCap @@ -94,6 +98,7 @@ proc restore*(ctx: Context) = let state = ctx.stateStack.pop() ctx.fillStyle = state.fillStyle ctx.strokeStyle = state.strokeStyle + ctx.globalAlpha = state.globalAlpha ctx.lineWidth = state.lineWidth ctx.miterLimit = state.miterLimit ctx.lineCap = state.lineCap @@ -113,9 +118,13 @@ proc restore*(ctx: Context) = else: # Otherwise draw to the root image ctx.image.draw(poppedLayer) -proc fill( - ctx: Context, image: Image, path: Path, windingRule: WindingRule -) {.inline.} = +proc fill(ctx: Context, image: Image, path: Path, windingRule: WindingRule) = + var image = image + + if ctx.globalAlpha != 1: + ctx.saveLayer() + image = ctx.layer + image.fillPath( path, ctx.fillStyle, @@ -123,7 +132,17 @@ proc fill( windingRule ) -proc stroke(ctx: Context, image: Image, path: Path) {.inline.} = + if ctx.globalAlpha != 1: + ctx.layer.applyOpacity(ctx.globalAlpha) + ctx.restore() + +proc stroke(ctx: Context, image: Image, path: Path) = + var image = image + + if ctx.globalAlpha != 1: + ctx.saveLayer() + image = ctx.layer + image.strokePath( path, ctx.strokeStyle, @@ -135,6 +154,10 @@ proc stroke(ctx: Context, image: Image, path: Path) {.inline.} = ctx.lineDash ) + if ctx.globalAlpha != 1: + ctx.layer.applyOpacity(ctx.globalAlpha) + ctx.restore() + proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = if ctx.font.typeface == nil: raise newException(PixieError, "No font has been set on this Context") @@ -145,6 +168,12 @@ proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = ctx.font.paint = ctx.fillStyle + var image = image + + if ctx.globalAlpha != 1: + ctx.saveLayer() + image = ctx.layer + image.fillText( ctx.font, text, @@ -152,6 +181,10 @@ proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = hAlign = ctx.textAlign ) + if ctx.globalAlpha != 1: + ctx.layer.applyOpacity(ctx.globalAlpha) + ctx.restore() + proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = if ctx.font.typeface == nil: raise newException(PixieError, "No font has been set on this Context") @@ -162,6 +195,12 @@ proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = ctx.font.paint = ctx.strokeStyle + var image = image + + if ctx.globalAlpha != 1: + ctx.saveLayer() + image = ctx.layer + image.strokeText( ctx.font, text, @@ -174,6 +213,10 @@ proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} = dashes = ctx.lineDash ) + if ctx.globalAlpha != 1: + ctx.layer.applyOpacity(ctx.globalAlpha) + ctx.restore() + proc beginPath*(ctx: Context) {.inline.} = ## Starts a new path by emptying the list of sub-paths. ctx.path = Path() diff --git a/tests/images/context/globalAlpha_1.png b/tests/images/context/globalAlpha_1.png new file mode 100644 index 0000000000000000000000000000000000000000..519b4c8488dc0bf67fca8b0c280315cfe4bdfa86 GIT binary patch literal 1079 zcmeAS@N?(olHy`uVBq!ia0y~yVAKJ!r*W_W$&~1=mlznB|9QGNhEy=Vy?c?j$v~nt zP*^-s#4Xvxtx&kv&tcL2qP@>mYO}q(A8_Z#y#M~YmYJiV_T5J&0f!9@5>2c}1auVI z5*Q6xxH}Xh9C!{gC2@!eJTBO(3QzuYU?{@3~xUr}H7Py}7ii`nveFmz-kEEZve#M;Q(#wIvujY?z@4 zj5VD!j)4(geL3~cuD