diff --git a/README.md b/README.md index 9e61cb5..3888018 100644 --- a/README.md +++ b/README.md @@ -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( diff --git a/examples/gradient.nim b/examples/gradient.nim index f86be4c..139dddb 100644 --- a/examples/gradient.nim +++ b/examples/gradient.nim @@ -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( diff --git a/examples/gradient.png b/examples/gradient.png index 47be463..c14c7f8 100644 Binary files a/examples/gradient.png and b/examples/gradient.png differ diff --git a/examples/text_spans.nim b/examples/text_spans.nim index e69d952..ebdb8f3 100644 --- a/examples/text_spans.nim +++ b/examples/text_spans.nim @@ -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)) diff --git a/examples/text_spans.png b/examples/text_spans.png index 6b0a04b..2f13dde 100644 Binary files a/examples/text_spans.png and b/examples/text_spans.png differ diff --git a/src/pixie/fileformats/svg.nim b/src/pixie/fileformats/svg.nim index 7b00d88..9797e17 100644 --- a/src/pixie/fileformats/svg.nim +++ b/src/pixie/fileformats/svg.nim @@ -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: diff --git a/src/pixie/images.nim b/src/pixie/images.nim index dede0bf..8eec6d4 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -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.} diff --git a/src/pixie/paints.nim b/src/pixie/paints.nim index 5d02a48..5565d4a 100644 --- a/src/pixie/paints.nim +++ b/src/pixie/paints.nim @@ -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. diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index 8c58ea2..e424eee 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -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 diff --git a/tests/fonts/diffs/image_paint_fill.png b/tests/fonts/diffs/image_paint_fill.png index aa1cbaa..c13b9b3 100644 Binary files a/tests/fonts/diffs/image_paint_fill.png and b/tests/fonts/diffs/image_paint_fill.png differ diff --git a/tests/fonts/rendered/image_paint_fill.png b/tests/fonts/rendered/image_paint_fill.png index e3fe51c..d9fbd48 100644 Binary files a/tests/fonts/rendered/image_paint_fill.png and b/tests/fonts/rendered/image_paint_fill.png differ diff --git a/tests/images/paths/gradientAngular.png b/tests/images/paths/gradientAngular.png index 6b20de1..1054f2f 100644 Binary files a/tests/images/paths/gradientAngular.png and b/tests/images/paths/gradientAngular.png differ diff --git a/tests/images/paths/gradientAngularOpacity.png b/tests/images/paths/gradientAngularOpacity.png index 2f298aa..93684c7 100644 Binary files a/tests/images/paths/gradientAngularOpacity.png and b/tests/images/paths/gradientAngularOpacity.png differ diff --git a/tests/images/paths/gradientLinear.png b/tests/images/paths/gradientLinear.png index 944cc45..c63e87e 100644 Binary files a/tests/images/paths/gradientLinear.png and b/tests/images/paths/gradientLinear.png differ diff --git a/tests/images/paths/gradientRadial.png b/tests/images/paths/gradientRadial.png index 80ff1f1..1738d73 100644 Binary files a/tests/images/paths/gradientRadial.png and b/tests/images/paths/gradientRadial.png differ diff --git a/tests/images/paths/opacityFill.png b/tests/images/paths/opacityFill.png index a51781e..f3e7f24 100644 Binary files a/tests/images/paths/opacityFill.png and b/tests/images/paths/opacityFill.png differ diff --git a/tests/images/paths/opacityStroke.png b/tests/images/paths/opacityStroke.png index aaefbfd..278d3d1 100644 Binary files a/tests/images/paths/opacityStroke.png and b/tests/images/paths/opacityStroke.png differ diff --git a/tests/images/svg/emojitwo.png b/tests/images/svg/emojitwo.png index c62ab90..169630c 100644 Binary files a/tests/images/svg/emojitwo.png and b/tests/images/svg/emojitwo.png differ diff --git a/tests/images/svg/noto-emoji.png b/tests/images/svg/noto-emoji.png index 4403d95..d2064a1 100644 Binary files a/tests/images/svg/noto-emoji.png and b/tests/images/svg/noto-emoji.png differ diff --git a/tests/images/svg/openmoji.png b/tests/images/svg/openmoji.png index 816a0d8..04d236d 100644 Binary files a/tests/images/svg/openmoji.png and b/tests/images/svg/openmoji.png differ diff --git a/tests/images/svg/twemoji.png b/tests/images/svg/twemoji.png index 9f9f0c7..1eff430 100644 Binary files a/tests/images/svg/twemoji.png and b/tests/images/svg/twemoji.png differ diff --git a/tests/megatest_emoji.nim b/tests/megatest_emoji.nim index 554e551..a1cf63a 100644 --- a/tests/megatest_emoji.nim +++ b/tests/megatest_emoji.nim @@ -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 ) diff --git a/tests/megatest_icons.nim b/tests/megatest_icons.nim index 3084917..a1e6a59 100644 --- a/tests/megatest_icons.nim +++ b/tests/megatest_icons.nim @@ -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 ) diff --git a/tests/test_contexts.nim b/tests/test_contexts.nim index 2ccb18e..8672bab 100644 --- a/tests/test_contexts.nim +++ b/tests/test_contexts.nim @@ -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") diff --git a/tests/test_fonts.nim b/tests/test_fonts.nim index 68f868c..11241ca 100644 --- a/tests/test_fonts.nim +++ b/tests/test_fonts.nim @@ -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 diff --git a/tests/test_paints.nim b/tests/test_paints.nim index eeb0267..e44b952 100644 --- a/tests/test_paints.nim +++ b/tests/test_paints.nim @@ -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 diff --git a/tests/test_paths.nim b/tests/test_paths.nim index d1bf887..51c37f0 100644 --- a/tests/test_paths.nim +++ b/tests/test_paths.nim @@ -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)