Search Docs
Loading...
Skip to content

Color Conversion

Convert colors between sRGB, CMYK, and spot color spaces programmatically in CE.SDK.

7 mins
estimated time
GitHub

CE.SDK supports sRGB, CMYK, and SpotColor values. Color conversion is a programmatic Engine API on Android, so use it when you build custom color interfaces, show print values, or prepare colors before export.

This guide covers converting colors to sRGB and CMYK, converting spot colors through their approximations, identifying Android color types, and checking how alpha and tint values are preserved.

Supported Color Spaces#

CE.SDK works with these Android color value types:

Color SpaceAndroid TypeUse Case
sRGBRGBAColor with r, g, b, a components from 0.0 to 1.0Screen display and previews
CMYKCMYKColor with c, m, y, k, tint components from 0.0 to 1.0Print workflows
SpotColorSpotColor with name, tint, and externalReferenceSpecialized printing with named inks

Use ColorSpace.SRGB or ColorSpace.CMYK as conversion targets. A SpotColor can be converted by using the RGB or CMYK approximation registered for its name.

Setting Up Colors#

Before converting a spot color, register an approximation for the spot color name. Android uses the overloaded setSpotColor(...) API for both RGB and CMYK approximations.

// Define a spot color with an RGB approximation for screen preview.
engine.editor.setSpotColor(
name = "Brand Red",
color = Color.fromRGBA(r = 0.95F, g = 0.25F, b = 0.21F, a = 1F),
)

Create color values with the Android Color factory methods. The sample uses the same sRGB, CMYK, and spot color values throughout the conversion steps.

val srgbColor = Color.fromRGBA(r = 0.2F, g = 0.4F, b = 0.9F, a = 1F)
val cmykColor = Color.fromCMYK(c = 0F, m = 0.8F, y = 0.95F, k = 0F, tint = 1F)
val spotColor = Color.fromSpotColor(
name = "Brand Red",
tint = 1F,
externalReference = "",
)

Converting to sRGB#

Use engine.editor.convertColorToColorSpace(color, ColorSpace.SRGB) when you need screen-display values. The returned value is a Color, so cast it to RGBAColor after converting to sRGB.

val cmykToSrgb = engine.editor.convertColorToColorSpace(
color = cmykColor,
colorSpace = ColorSpace.SRGB,
) as RGBAColor
val spotToSrgb = engine.editor.convertColorToColorSpace(
color = spotColor,
colorSpace = ColorSpace.SRGB,
) as RGBAColor
println("CMYK converted to sRGB: $cmykToSrgb")
println("Spot color converted to sRGB: $spotToSrgb")

CMYK colors convert to RGBAColor components. Spot colors use the registered RGB approximation for their spot color name. When the source color has a tint below 1.0, the returned sRGB color keeps a = 1.0 and blends its RGB components toward white.

Converting to CMYK#

Use engine.editor.convertColorToColorSpace(color, ColorSpace.CMYK) when a print workflow needs CMYK components. For spot colors, register a CMYK approximation before converting to CMYK.

val srgbToCmyk = engine.editor.convertColorToColorSpace(
color = srgbColor,
colorSpace = ColorSpace.CMYK,
) as CMYKColor
// Add a CMYK approximation before converting the spot color for print output.
engine.editor.setSpotColor(
name = "Brand Red",
color = Color.fromCMYK(c = 0F, m = 0.85F, y = 0.9F, k = 0.05F, tint = 1F),
)
val spotToCmyk = engine.editor.convertColorToColorSpace(
color = spotColor,
colorSpace = ColorSpace.CMYK,
) as CMYKColor
println("sRGB converted to CMYK: $srgbToCmyk")
println("Spot color converted to CMYK: $spotToCmyk")

Identifying Color Types#

Android exposes color values as a sealed Color hierarchy. Use Kotlin type checks before reading type-specific properties.

val detectedTypes = listOf(srgbColor, cmykColor, spotColor).map { color ->
when (color) {
is RGBAColor -> "sRGB"
is CMYKColor -> "CMYK"
is SpotColor -> "SpotColor"
}
}

This lets you branch on RGBAColor, CMYKColor, and SpotColor before reading their component properties.

Handling Tint and Alpha#

On Android, alpha and tint are not simply copied between target fields. Non-black sRGB colors convert to CMYK with tint = 1.0 even if the source has alpha, while pure black sRGB uses the source alpha as the CMYK tint. Tinted CMYK and spot colors convert to full-opacity sRGB previews, but their RGB components are blended toward white based on the tint. For example, pure CMYK red with tint = 0.5 converts to a pink preview with r = 1.0, g = 0.5, b = 0.5, and a = 1.0.

SourceTargetTransformation
sRGB alpha (non-black)CMYKThe converted CMYK color uses tint = 1.0
sRGB alpha (pure black)CMYKThe converted CMYK color uses tint = source alpha
CMYK tintsRGBRGB components blend toward white and a = 1.0
SpotColor tintsRGBRGB components blend toward white and a = 1.0
SpotColor tintCMYKThe converted CMYK color uses tint = source tint

The sample below uses a non-black transparent sRGB color, so its converted CMYK color keeps tint = 1.0.

val transparentSrgb = Color.fromRGBA(r = 0.2F, g = 0.4F, b = 0.9F, a = 0.5F)
val transparentSrgbToCmyk = engine.editor.convertColorToColorSpace(
color = transparentSrgb,
colorSpace = ColorSpace.CMYK,
) as CMYKColor
val tintedCmyk = Color.fromCMYK(c = 0F, m = 0.8F, y = 0.95F, k = 0F, tint = 0.5F)
val tintedCmykPreview = engine.editor.convertColorToColorSpace(
color = tintedCmyk,
colorSpace = ColorSpace.SRGB,
) as RGBAColor
val tintedSpotColor = Color.fromSpotColor(
name = "Brand Red",
tint = 0.4F,
externalReference = "",
)
val tintedSpotPreview = engine.editor.convertColorToColorSpace(
color = tintedSpotColor,
colorSpace = ColorSpace.SRGB,
) as RGBAColor
val tintedSpotToCmyk = engine.editor.convertColorToColorSpace(
color = tintedSpotColor,
colorSpace = ColorSpace.CMYK,
) as CMYKColor

Practical Use Cases#

Building a Color Picker#

When a custom color picker needs screen preview values, convert the design color to sRGB and display the returned RGBAColor components.

val colorFromDesign: Color = spotColor
val pickerPreviewColor = engine.editor.convertColorToColorSpace(
color = colorFromDesign,
colorSpace = ColorSpace.SRGB,
) as RGBAColor
// Display pickerPreviewColor.r, pickerPreviewColor.g, pickerPreviewColor.b, and pickerPreviewColor.a.

Export Preparation#

Before a print-oriented export, check whether the color is already CMYK. Convert non-CMYK colors to ColorSpace.CMYK before showing or storing print values.

val colorForExport: Color = srgbColor
val printColor = if (colorForExport is CMYKColor) {
colorForExport
} else {
engine.editor.convertColorToColorSpace(
color = colorForExport,
colorSpace = ColorSpace.CMYK,
) as CMYKColor
}

Troubleshooting#

IssueCauseSolution
Spot color converts to an unexpected valueThe spot color has no approximation for the target color spaceCall setSpotColor(...) with an RGBAColor or CMYKColor before conversion
Colors differ after round-trippingColor conversion is not always losslessAvoid assuming that converting sRGB to CMYK and back returns the exact original value
Type-specific properties are unavailableconvertColorToColorSpace(...) returns the base Color typeCast after converting, or check the type with Kotlin is checks

API Reference#

APIPurpose
engine.editor.convertColorToColorSpace(color=_, colorSpace=_)Converts a color to ColorSpace.SRGB or ColorSpace.CMYK
engine.editor.setSpotColor(name=_, color=Color.fromRGBA(r=_, g=_, b=_, a=_))Defines or updates an RGB approximation for a spot color
engine.editor.setSpotColor(name=_, color=Color.fromCMYK(c=_, m=_, y=_, k=_, tint=_))Defines or updates a CMYK approximation for a spot color
Color.fromRGBA(r=_, g=_, b=_, a=_)Creates an sRGB color value
Color.fromCMYK(c=_, m=_, y=_, k=_, tint=_)Creates a CMYK color value
Color.fromSpotColor(name=_, tint=_, externalReference=_)Creates a spot color reference that uses the registered approximation for its name