Search Docs
Loading...
Skip to content

Color Effects

Remap the colors of a block with the tint, fill, tritone, levels, invert, threshold and shift channels effects.

6 mins
estimated time
GitHub

A color effect changes the color of every pixel and leaves each pixel where it is. Color effects use the same effect API as all other effects. You create an effect with createEffect, set its properties and add it to a block with appendEffect. Scenes that use these effects do not open in earlier versions of CE.SDK. To insert, disable or remove effects, see Apply a Filter or Effect.

The Color Effects#

The Supported Filters and Effects guide lists the properties and default values of every type.

Type What it does
EffectType.Tint Maps the luminance of each pixel onto a gradient between two colors
EffectType.Fill Replaces the color of every pixel with one color and keeps the alpha
EffectType.Tritone Maps dark, middle and light values to three colors
EffectType.Levels Maps each channel from an input range to an output range with a gamma curve
EffectType.Invert Inverts one channel, or a group of channels in the RGB, HSL or YIQ color space
EffectType.Threshold Turns every pixel black or white at a luminance level
EffectType.ShiftChannels Replaces each color channel with a value taken from the input pixel

Creating the Image Blocks#

We add four copies of one image to the page. Each section below adds its effects to one of these blocks.

// Adds a 360 x 270 image block to the page at a position
val imageUri = defaultBaseUri.buildUpon()
.appendPath("ly.img.image")
.appendPath("images")
.appendPath("sample_1.jpg")
.build()
fun addImageBlock(
x: Float,
y: Float,
): DesignBlock {
val block = engine.block.create(DesignBlockType.Graphic)
engine.block.setShape(block = block, shape = engine.block.createShape(ShapeType.Rect))
val fill = engine.block.createFill(FillType.Image)
engine.block.setUri(block = fill, property = "fill/image/imageFileURI", value = imageUri)
engine.block.setFill(block = block, fill = fill)
engine.block.setWidth(block = block, value = 360F)
engine.block.setHeight(block = block, value = 270F)
engine.block.setPositionX(block = block, value = x)
engine.block.setPositionY(block = block, value = y)
engine.block.appendChild(parent = page, child = block)
return block
}
val stackBlock = addImageBlock(x = 30F, y = 30F)
val tintBlock = addImageBlock(x = 410F, y = 30F)
val thresholdBlock = addImageBlock(x = 30F, y = 320F)
val shiftChannelsBlock = addImageBlock(x = 410F, y = 320F)

Stacking Levels and Tritone#

A block applies its effects in the order of its effect list. appendEffect adds an effect to the end of the list. Here Levels runs first and maps the input range from 0.1 to 0.9 onto the full range. Then Tritone maps black, mid gray and white to three colors.

// Levels runs first and stretches the input range
val levels = engine.block.createEffect(EffectType.Levels)
engine.block.setFloat(block = levels, property = "effect/levels/rgbInBlack", value = 0.1F)
engine.block.setFloat(block = levels, property = "effect/levels/rgbInWhite", value = 0.9F)
engine.block.appendEffect(block = stackBlock, effectBlock = levels)
// Tritone runs second and maps black, mid gray and white to three colors
val tritone = engine.block.createEffect(EffectType.Tritone)
engine.block.setColor(
block = tritone,
property = "effect/tritone/shadows",
value = Color.fromRGBA(r = 0.1F, g = 0.1F, b = 0.3F),
)
engine.block.setColor(
block = tritone,
property = "effect/tritone/midtones",
value = Color.fromRGBA(r = 0.8F, g = 0.3F, b = 0.4F),
)
engine.block.setColor(
block = tritone,
property = "effect/tritone/highlights",
value = Color.fromRGBA(r = 1F, g = 0.9F, b = 0.7F),
)
engine.block.appendEffect(block = stackBlock, effectBlock = tritone)

If you reverse the order, the result changes: Levels then stretches the three tritone colors instead of the image.

Tint, Threshold and Shift Channels#

The other effects in this guide need one or two properties each.

Tint maps black to mapBlackTo and white to mapWhiteTo. amount mixes the input (0) with the tinted color (1).

// Map black to dark blue and white to warm yellow, at 80% strength
val tint = engine.block.createEffect(EffectType.Tint)
engine.block.setColor(
block = tint,
property = "effect/tint/mapBlackTo",
value = Color.fromRGBA(r = 0.05F, g = 0.1F, b = 0.35F),
)
engine.block.setColor(
block = tint,
property = "effect/tint/mapWhiteTo",
value = Color.fromRGBA(r = 1F, g = 0.85F, b = 0.4F),
)
engine.block.setFloat(block = tint, property = "effect/tint/amount", value = 0.8F)
engine.block.appendEffect(block = tintBlock, effectBlock = tint)

Threshold turns a pixel white when its luminance is at or above level, from 0 to 1. All other pixels turn black.

// Pixels with a luminance of 0.45 or more turn white, all others black
val threshold = engine.block.createEffect(EffectType.Threshold)
engine.block.setFloat(block = threshold, property = "effect/threshold/level", value = 0.45F)
engine.block.appendEffect(block = thresholdBlock, effectBlock = threshold)

ShiftChannels has one option property for each output channel: takeRedFrom, takeGreenFrom, takeBlueFrom and takeAlphaFrom. Each one takes Red, Green, Blue, Alpha, Luminance, Hue, Lightness, Saturation, FullOn or FullOff. Here the red and blue channels swap.

// Swap the red and the blue channel
val shiftChannels = engine.block.createEffect(EffectType.ShiftChannels)
engine.block.setEnum(block = shiftChannels, property = "effect/shift_channels/takeRedFrom", value = "Blue")
engine.block.setEnum(block = shiftChannels, property = "effect/shift_channels/takeBlueFrom", value = "Red")
engine.block.appendEffect(block = shiftChannelsBlock, effectBlock = shiftChannels)

Exporting the Result#

We export the page as a PNG to see all four versions of the image.

val previewPng = engine.block.export(block = page, mimeType = MimeType.PNG)

API Reference#

Method Description
engine.block.create(blockType=DesignBlockType.Graphic) Create the graphic block for an image
engine.block.createShape(type=ShapeType.Rect) Create the rect shape of the block
engine.block.setShape(block=_, shape=_) Give the block its shape
engine.block.createFill(fillType=FillType.Image) Create an image fill
engine.block.setUri(block=_, property="fill/image/imageFileURI", value=_) Set the image of the fill
engine.block.setFill(block=_, fill=_) Attach the fill to the block
engine.block.setWidth(block=_, value=_) Set the width of the block
engine.block.setHeight(block=_, value=_) Set the height of the block
engine.block.setPositionX(block=_, value=_) Set the horizontal position of the block
engine.block.setPositionY(block=_, value=_) Set the vertical position of the block
engine.block.appendChild(parent=_, child=_) Add the block to the page
engine.block.createEffect(type=_) Create an effect, for example EffectType.Tritone
engine.block.appendEffect(block=_, effectBlock=_) Add an effect to the end of the effect list of a block
engine.block.setFloat(block=_, property="effect/threshold/level", value=_) Set a number property of an effect
engine.block.setColor(block=_, property="effect/tint/mapBlackTo", value=_) Set a color property of an effect
engine.block.setEnum(block=_, property="effect/shift_channels/takeRedFrom", value=_) Set an option property of an effect
engine.block.export(block=_, mimeType=_) Export the page with all effects

Next Steps#