[orx-color] Add ColorRGBa.convertTo<>() and simplify shiftHue, saturate, mix

This commit is contained in:
Edwin Jakobs
2023-12-08 11:24:53 +01:00
parent 0b27dc735c
commit ee9a5f5463

View File

@@ -3,7 +3,9 @@ package org.openrndr.extra.color.tools
import org.openrndr.color.*
import org.openrndr.extra.color.spaces.*
inline fun <reified T : AlgebraicColor<T>> ColorRGBa.blendWith(other: ColorRGBa, steps: Int): Sequence<ColorRGBa> {
inline fun <reified T> ColorRGBa.blendWith(other: ColorRGBa, steps: Int): Sequence<ColorRGBa>
where T : AlgebraicColor<T>,
T: ColorModel<T> {
return sequence {
for (step in 0 until steps) {
yield(mixedWith<T>(other, step / (steps - 1.0)))
@@ -11,30 +13,36 @@ inline fun <reified T : AlgebraicColor<T>> ColorRGBa.blendWith(other: ColorRGBa,
}
}
inline fun <reified T : AlgebraicColor<T>> 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 <reified T : ColorModel<T>> 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 <reified T> ColorRGBa.mixedWith(other: ColorRGBa, factor: Double): ColorRGBa
where T : AlgebraicColor<T>, T : ColorModel<T> {
val source = convertTo<T>()
val target = other.convertTo<T>()
val mixed = source.mix(target, factor).toRGBa()
return if (mixed.linearity.isEquivalent(linearity)) {
mixed
@@ -47,30 +55,14 @@ inline fun <reified T : AlgebraicColor<T>> ColorRGBa.mixedWith(other: ColorRGBa,
mixed
}
}
return this
}
inline fun <reified T> ColorRGBa.saturate(factor: Double): ColorRGBa
where T : SaturatableColor<T>,
T : ColorModel<T>,
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<T>().saturate(factor).toRGBa()
return if (saturated.linearity.isEquivalent(linearity)) {
saturated
} else {
@@ -85,22 +77,10 @@ inline fun <reified T> ColorRGBa.saturate(factor: Double): ColorRGBa
}
inline fun <reified T> ColorRGBa.shiftHue(degrees: Double): ColorRGBa where
T : HueShiftableColor<T>, 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>,
T : ColorModel<T>,
T : ConvertibleToColorRGBa {
val converted = convertTo<T>()
val shifted = (converted.shiftHue(degrees) as ConvertibleToColorRGBa).toRGBa()
return if (shifted.linearity.isEquivalent(linearity)) {
shifted