diff --git a/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt b/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt index 364a3e52..0da1c828 100644 --- a/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt +++ b/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt @@ -3,7 +3,9 @@ package org.openrndr.extra.color.tools import org.openrndr.color.* import org.openrndr.extra.color.spaces.* -inline fun > ColorRGBa.blendWith(other: ColorRGBa, steps: Int): Sequence { +inline fun ColorRGBa.blendWith(other: ColorRGBa, steps: Int): Sequence + where T : AlgebraicColor, + T: ColorModel { return sequence { for (step in 0 until steps) { yield(mixedWith(other, step / (steps - 1.0))) @@ -11,30 +13,36 @@ inline fun > ColorRGBa.blendWith(other: ColorRGBa, } } -inline fun > ColorRGBa.mixedWith(other: ColorRGBa, factor: Double): ColorRGBa { - - val mixed = when (T::class) { - ColorHSLa::class -> this.toHSLa().mix(other.toHSLa(), factor) - ColorHSVa::class -> this.toHSVa().mix(other.toHSVa(), factor) - ColorRGBa::class -> this.mix(other, factor) - - ColorHSLUVa::class -> this.toHSLUVa().mix(other.toHSLUVa(), factor) - ColorOKLABa::class -> this.toOKLABa().mix(other.toOKLABa(), factor) - ColorOKLCHa::class -> this.toOKLCHa().mix(other.toOKLCHa(), factor) - ColorOKHSLa::class -> this.toOKHSLa().mix(other.toOKHSLa(), factor) - ColorOKHSVa::class -> this.toOKHSVa().mix(other.toOKHSVa(), factor) - - ColorLABa::class -> this.toLABa().mix(other.toLABa(), factor) - ColorLUVa::class -> this.toLUVa().mix(other.toLUVa(), factor) - ColorLCHABa::class -> this.toLCHABa().mix(other.toLCHABa(), factor) - ColorLCHUVa::class -> this.toLCHUVa().mix(other.toLCHUVa(), factor) - ColorOKHSLa::class -> this.toOKHSLa().mix(other.toOKHSLa(), factor) - ColorXYZa::class -> this.toXYZa().mix(other.toXYZa(), factor) - ColorXSLUVa::class -> this.toXSLUVa().mix(other.toXSLUVa(), factor) - ColorXSVa::class -> this.toXSVa().mix(other.toXSVa(), factor) - ColorXSLa::class -> this.toXSLa().mix(other.toXSLa(), factor) +inline fun > ColorRGBa.convertTo(): T { + val converted = when (T::class) { + ColorHSLa::class -> this.toHSLa() + ColorHSVa::class -> this.toHSVa() + ColorRGBa::class -> this + ColorHSLUVa::class -> this.toHSLUVa() + ColorOKLABa::class -> this.toOKLABa() + ColorOKLCHa::class -> this.toOKLCHa() + ColorOKHSLa::class -> this.toOKHSLa() + ColorOKHSVa::class -> this.toOKHSVa() + ColorLABa::class -> this.toLABa() + ColorLUVa::class -> this.toLUVa() + ColorLCHABa::class -> this.toLCHABa() + ColorLCHUVa::class -> this.toLCHUVa() + ColorOKHSLa::class -> this.toOKHSLa() + ColorXYZa::class -> this.toXYZa() + ColorXSLUVa::class -> this.toXSLUVa() + ColorXSVa::class -> this.toXSVa() + ColorXSLa::class -> this.toXSLa() else -> error("color model ${T::class} not supported") - }.toRGBa() + } + return converted as T +} + +inline fun ColorRGBa.mixedWith(other: ColorRGBa, factor: Double): ColorRGBa + where T : AlgebraicColor, T : ColorModel { + + val source = convertTo() + val target = other.convertTo() + val mixed = source.mix(target, factor).toRGBa() return if (mixed.linearity.isEquivalent(linearity)) { mixed @@ -47,30 +55,14 @@ inline fun > ColorRGBa.mixedWith(other: ColorRGBa, mixed } } - - return this } inline fun ColorRGBa.saturate(factor: Double): ColorRGBa where T : SaturatableColor, + T : ColorModel, T : ConvertibleToColorRGBa { - val converted = when (T::class) { - ColorHPLUVa::class -> toHPLUVa() - ColorHSLUVa::class -> toHSLUVa() - ColorHSLa::class -> toHSLa() - ColorHSVa::class -> toHSVa() - ColorXSLa::class -> toXSLa() - ColorXSVa::class -> toXSVa() - ColorOKLCHa::class -> toOKLCHa() - ColorOKHSLa::class -> toOKHSLa() - ColorOKHSVa::class -> toOKHSVa() - ColorXSLUVa::class -> toXSLUVa() - ColorOKLCHa::class -> toOKLCHa() - else -> error("Color space ${T::class} not supported") - } - val saturated = (converted.saturate(factor) as ConvertibleToColorRGBa).toRGBa() - + val saturated = convertTo().saturate(factor).toRGBa() return if (saturated.linearity.isEquivalent(linearity)) { saturated } else { @@ -85,22 +77,10 @@ inline fun ColorRGBa.saturate(factor: Double): ColorRGBa } inline fun ColorRGBa.shiftHue(degrees: Double): ColorRGBa where - T : HueShiftableColor, T : ConvertibleToColorRGBa { - val converted = when (T::class) { - ColorHSLa::class -> toHSLa() - ColorHSVa::class -> toHSVa() - ColorXSLa::class -> toXSLa() - ColorXSVa::class -> toXSVa() - ColorOKLCHa::class -> toOKLCHa() - ColorLCHABa::class -> toLCHABa() - ColorLCHUVa::class -> toLCHABa() - ColorOKHSLa::class -> toOKHSLa() - ColorOKHSVa::class -> toOKHSVa() - ColorHPLUVa::class -> toHPLUVa() - ColorHSLUVa::class -> toHSLUVa() - ColorXSLUVa::class -> toXSLUVa() - else -> error("Color space ${T::class} not supported") - } + T : HueShiftableColor, + T : ColorModel, + T : ConvertibleToColorRGBa { + val converted = convertTo() val shifted = (converted.shiftHue(degrees) as ConvertibleToColorRGBa).toRGBa() return if (shifted.linearity.isEquivalent(linearity)) { shifted