explicit names instead of alphy shorthand
This commit is contained in:
parent
45fe8bd9ea
commit
9e834ce8e4
3 changed files with 13 additions and 13 deletions
|
@ -16,14 +16,14 @@ proc lerp*(a, b: ColorRGBA, t: float32): ColorRGBA {.inline.} =
|
|||
result.b = ((a.b.uint32 * (255 - x) + b.b.uint32 * x) div 255).uint8
|
||||
result.a = ((a.a.uint32 * (255 - x) + b.a.uint32 * x) div 255).uint8
|
||||
|
||||
proc premultiplyAlpha*(c: ColorRGBA): ColorRGBA {.inline.} =
|
||||
proc toPremultipliedAlpha*(c: ColorRGBA): ColorRGBA {.inline.} =
|
||||
## Converts a color to premultiplied alpha from straight alpha.
|
||||
result.r = ((c.r.uint16 * c.a.uint16) div 255).uint8
|
||||
result.g = ((c.g.uint16 * c.a.uint16) div 255).uint8
|
||||
result.b = ((c.b.uint16 * c.a.uint16) div 255).uint8
|
||||
result.a = c.a
|
||||
|
||||
proc straightenAlpha*(c: ColorRGBA): ColorRGBA {.inline.} =
|
||||
proc toStraightAlpha*(c: ColorRGBA): ColorRGBA {.inline.} =
|
||||
## Converts a color to from premultiplied alpha to straight alpha.
|
||||
result = c
|
||||
if result.a != 0 and result.a != 255:
|
||||
|
@ -38,14 +38,14 @@ func lerp*(a, b: Color, v: float32): Color {.inline.} =
|
|||
result.b = lerp(a.b, b.b, v)
|
||||
result.a = lerp(a.a, b.a, v)
|
||||
|
||||
proc toAlphy*(c: Color): Color {.inline.} =
|
||||
proc toPremultipliedAlpha*(c: Color): Color {.inline.} =
|
||||
## Converts a color to premultiplied alpha from straight.
|
||||
result.r = c.r * c.a
|
||||
result.g = c.g * c.a
|
||||
result.b = c.b * c.a
|
||||
result.a = c.a
|
||||
|
||||
proc fromAlphy*(c: Color): Color {.inline.} =
|
||||
proc toStraightAlpha*(c: Color): Color {.inline.} =
|
||||
## Converts a color to from premultiplied alpha to straight.
|
||||
if c.a == 0:
|
||||
return
|
||||
|
|
|
@ -175,7 +175,7 @@ proc magnifyBy2*(image: Image): Image =
|
|||
when defined(release):
|
||||
{.pop.}
|
||||
|
||||
proc toAlphy*(image: Image) =
|
||||
proc toPremultipliedAlpha*(image: Image) =
|
||||
## Converts an image to premultiplied alpha from straight.
|
||||
var i: int
|
||||
when defined(amd64) and not defined(pixieNoSimd):
|
||||
|
@ -217,7 +217,7 @@ proc toAlphy*(image: Image) =
|
|||
c.b = ((c.b.uint32 * c.a.uint32) div 255).uint8
|
||||
image.data[j] = c
|
||||
|
||||
proc fromAlphy*(image: Image) =
|
||||
proc toStraightAlpha*(image: Image) =
|
||||
## Converts an image from premultiplied alpha to straight alpha.
|
||||
## This is expensive for large images.
|
||||
for c in image.data.mitems:
|
||||
|
@ -256,16 +256,16 @@ proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA {.inline.} =
|
|||
minY = y.floor.int
|
||||
difY = y - y.floor
|
||||
|
||||
vX0Y0 = image[minX, minY].premultiplyAlpha()
|
||||
vX1Y0 = image[minX + 1, minY].premultiplyAlpha()
|
||||
vX0Y1 = image[minX, minY + 1].premultiplyAlpha()
|
||||
vX1Y1 = image[minX + 1, minY + 1].premultiplyAlpha()
|
||||
vX0Y0 = image[minX, minY].toPremultipliedAlpha()
|
||||
vX1Y0 = image[minX + 1, minY].toPremultipliedAlpha()
|
||||
vX0Y1 = image[minX, minY + 1].toPremultipliedAlpha()
|
||||
vX1Y1 = image[minX + 1, minY + 1].toPremultipliedAlpha()
|
||||
|
||||
bottomMix = lerp(vX0Y0, vX1Y0, difX)
|
||||
topMix = lerp(vX0Y1, vX1Y1, difX)
|
||||
finalMix = lerp(bottomMix, topMix, difY)
|
||||
|
||||
finalMix.straightenAlpha()
|
||||
finalMix.toStraightAlpha()
|
||||
|
||||
proc resize*(srcImage: Image, width, height: int): Image =
|
||||
result = newImage(width, height)
|
||||
|
|
|
@ -19,13 +19,13 @@ block:
|
|||
block:
|
||||
let image = newImage(10, 10)
|
||||
image.fill(rgba(255, 0, 0, 128))
|
||||
image.toAlphy()
|
||||
image.toPremultipliedAlpha()
|
||||
doAssert image[9, 9] == rgba(128, 0, 0, 128)
|
||||
|
||||
block:
|
||||
let image = newImage(10, 10)
|
||||
image.fill(rgba(128, 0, 0, 128))
|
||||
image.fromAlphy()
|
||||
image.toStraightAlpha()
|
||||
doAssert image[9, 9] == rgba(254, 0, 0, 128)
|
||||
|
||||
block:
|
||||
|
|
Loading…
Reference in a new issue