From af524b8e4203f85d237a460dbbe11ab52913fc58 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 22 Oct 2021 07:38:59 +0200 Subject: [PATCH] [orx-color] Make ColorOKHSL, ColorOKHSV AlgabraicCcolor, fix package name --- .../commonMain/kotlin/palettes/Palettes.kt | 2 + .../commonMain/kotlin/spaces/ColorOKHSL.kt | 55 ++++++++++++------- .../commonMain/kotlin/spaces/ColorOKHSV.kt | 25 +++++++-- orx-color/src/demo/kotlin/DemoColorRange03.kt | 7 +-- .../src/commonMain/kotlin/ColorspaceHelper.kt | 2 +- .../src/commonMain/kotlin/LinearGradient.kt | 2 +- .../commonMain/kotlin/NPointLinearGradient.kt | 2 +- .../src/commonMain/kotlin/RadialGradient.kt | 2 +- .../kotlin/drawers/BezierPatchDrawer.kt | 2 +- 9 files changed, 65 insertions(+), 34 deletions(-) diff --git a/orx-color/src/commonMain/kotlin/palettes/Palettes.kt b/orx-color/src/commonMain/kotlin/palettes/Palettes.kt index 14219894..e105e9e4 100644 --- a/orx-color/src/commonMain/kotlin/palettes/Palettes.kt +++ b/orx-color/src/commonMain/kotlin/palettes/Palettes.kt @@ -53,6 +53,8 @@ class ColorSequence(val colors: List>) { is ColorLCHABa -> right.second.toRGBa().toLCHABa().mix(l, nt).toRGBa() is ColorOKLABa -> right.second.toRGBa().toOKLABa().mix(l, nt).toRGBa() is ColorOKLCHa -> right.second.toRGBa().toOKLCHa().mix(l, nt).toRGBa() + is ColorOKHSLa -> right.second.toRGBa().toOKHSLa().mix(l, nt).toRGBa() + is ColorOKHSVa -> right.second.toRGBa().toOKHSVa().mix(l, nt).toRGBa() else -> error("unsupported color space: ${l::class}") }.toSRGB() } diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSL.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSL.kt index 75710fcc..81e8c557 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSL.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSL.kt @@ -1,6 +1,7 @@ package org.openrndr.extra.color.spaces import org.openrndr.color.* +import org.openrndr.math.mixAngle import kotlin.math.* data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, val a: Double = 1.0) : @@ -8,6 +9,7 @@ data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, val a: Doubl OpacifiableColor, SaturatableColor, ShadableColor, + AlgebraicColor, ConvertibleToColorRGBa { companion object { @@ -47,17 +49,13 @@ data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, val a: Doubl } override fun toRGBa(): ColorRGBa { - if (l == 1.0) - { + if (l == 1.0) { ColorRGBa(1.0, 1.0, 1.0, a) - } - - else if (l == 0.0) - { + } else if (l == 0.0) { ColorRGBa(0.0, 0.0, 0.0, a) } - val a_ = cos(2*PI*h); - val b_ = sin(2*PI*h); + val a_ = cos(2 * PI * h); + val b_ = sin(2 * PI * h); val L = toe_inv(l); val Cs = get_Cs(L, a_, b_); @@ -71,22 +69,19 @@ data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, val a: Doubl val k_0: Double val k_1: Double val k_2: Double - if (s < 0.8) - { - t = 1.25*s; + if (s < 0.8) { + t = 1.25 * s; k_0 = 0.0 - k_1 = 0.8*C_0; - k_2 = (1-k_1/C_mid); - } - else - { - t = 5*(s-0.8); + k_1 = 0.8 * C_0; + k_2 = (1 - k_1 / C_mid); + } else { + t = 5 * (s - 0.8); k_0 = C_mid; - k_1 = 0.2*C_mid*C_mid*1.25*1.25/C_0; - k_2 = (1 - (k_1)/(C_max - C_mid)); + k_1 = 0.2 * C_mid * C_mid * 1.25 * 1.25 / C_0; + k_2 = (1 - (k_1) / (C_max - C_mid)); } - C = k_0 + t*k_1/(1-k_2*t); + C = k_0 + t * k_1 / (1 - k_2 * t); // If we would only use one of the Cs: //C = s*C_0; @@ -119,6 +114,24 @@ data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, val a: Doubl return copy(l = l * factor) } + override fun minus(right: ColorOKHSLa) = + copy(h = h - right.h, s = s - right.s, l = l - right.l, a = a - right.a) + + override fun plus(right: ColorOKHSLa) = + copy(h = h + right.h, s = s + right.s, l = l + right.l, a = a + right.a) + + override fun times(scale: Double): ColorOKHSLa = copy(h = h * scale, s = s * scale, l = l * scale, a = a * scale) + + override fun mix(other: ColorOKHSLa, factor: Double): ColorOKHSLa { + val sx = factor.coerceIn(0.0, 1.0) + return ColorOKHSLa( + mixAngle(h * 360.0, other.h * 360.0, sx) / 360.0, + (1.0 - sx) * s + sx * other.s, + (1.0 - sx) * l + sx * other.l, + (1.0 - sx) * a + sx * other.a + ) + } + } -fun ColorRGBa.toOKHSLa() : ColorOKHSLa = ColorOKHSLa.fromColorRGBa(this) \ No newline at end of file +fun ColorRGBa.toOKHSLa(): ColorOKHSLa = ColorOKHSLa.fromColorRGBa(this) \ No newline at end of file diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSV.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSV.kt index b8950d81..1f806f10 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSV.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSV.kt @@ -1,6 +1,7 @@ package org.openrndr.extra.color.spaces import org.openrndr.color.* +import org.openrndr.math.mixAngle import kotlin.math.* @@ -9,6 +10,7 @@ data class ColorOKHSVa(val h: Double, val s: Double, val v: Double, val a: Doubl OpacifiableColor, SaturatableColor, ShadableColor, + AlgebraicColor, ConvertibleToColorRGBa { companion object { @@ -88,12 +90,10 @@ data class ColorOKHSVa(val h: Double, val s: Double, val v: Double, val a: Doubl C *= scale_L; return ColorOKLABa(L, C * a_, C * b_).toRGBa().toSRGB() - - } override fun shiftHue(shiftInDegrees: Double): ColorOKHSVa { - val normalizedShift = shiftInDegrees/360.0 + val normalizedShift = shiftInDegrees / 360.0 return copy(h = h + normalizedShift) } @@ -109,6 +109,23 @@ data class ColorOKHSVa(val h: Double, val s: Double, val v: Double, val a: Doubl return copy(v = v * factor) } + override fun minus(right: ColorOKHSVa) = + copy(h = h - right.h, s = s - right.s, v = v - right.v, a = a - right.a) + + override fun plus(right: ColorOKHSVa) = + copy(h = h + right.h, s = s + right.s, v = v + right.v, a = a + right.a) + + override fun times(scale: Double): ColorOKHSVa = copy(h = h * scale, s = s * scale, v = v * scale, a = a * scale) + + override fun mix(other: ColorOKHSVa, factor: Double): ColorOKHSVa { + val sx = factor.coerceIn(0.0, 1.0) + return ColorOKHSVa( + mixAngle(h * 360.0, other.h * 360.0, sx) / 360.0, + (1.0 - sx) * s + sx * other.s, + (1.0 - sx) * v + sx * other.v, + (1.0 - sx) * a + sx * other.a + ) + } } -fun ColorRGBa.toOKHSVa() : ColorOKHSVa = ColorOKHSVa.fromColorRGBa(this) \ No newline at end of file +fun ColorRGBa.toOKHSVa(): ColorOKHSVa = ColorOKHSVa.fromColorRGBa(this) \ No newline at end of file diff --git a/orx-color/src/demo/kotlin/DemoColorRange03.kt b/orx-color/src/demo/kotlin/DemoColorRange03.kt index f4b85a18..2529ed28 100644 --- a/orx-color/src/demo/kotlin/DemoColorRange03.kt +++ b/orx-color/src/demo/kotlin/DemoColorRange03.kt @@ -2,11 +2,8 @@ import org.openrndr.application import org.openrndr.color.ColorRGBa import org.openrndr.draw.loadFont import org.openrndr.extensions.SingleScreenshot +import org.openrndr.extra.color.spaces.* import org.openrndr.extras.color.palettes.rangeTo -import org.openrndr.extra.color.spaces.toHSLUVa -import org.openrndr.extra.color.spaces.toOKLABa -import org.openrndr.extra.color.spaces.toOKLCHa -import org.openrndr.extra.color.spaces.toXSLUVa fun main() { application { @@ -27,6 +24,8 @@ fun main() { "LCh(ab)" to (colorA.toLCHABa()..colorB.toLCHABa() blend stepCount), "OKLab" to (colorA.toOKLABa()..colorB.toOKLABa() blend stepCount), "OKLCh" to (colorA.toOKLCHa()..colorB.toOKLCHa() blend stepCount), + "OKHSV" to (colorA.toOKHSVa()..colorB.toOKHSVa() blend stepCount), + "OKHSL" to (colorA.toOKHSLa()..colorB.toOKHSLa() blend stepCount), "HSLUV" to (colorA.toHSLUVa()..colorB.toHSLUVa() blend stepCount), "XSLUV" to (colorA.toXSLUVa()..colorB.toXSLUVa() blend stepCount), ) diff --git a/orx-shade-styles/src/commonMain/kotlin/ColorspaceHelper.kt b/orx-shade-styles/src/commonMain/kotlin/ColorspaceHelper.kt index 9dc78e23..82349955 100644 --- a/orx-shade-styles/src/commonMain/kotlin/ColorspaceHelper.kt +++ b/orx-shade-styles/src/commonMain/kotlin/ColorspaceHelper.kt @@ -1,7 +1,7 @@ package org.openrndr.extra.shadestyles import org.openrndr.color.ColorRGBa -import org.openrndr.extras.color.spaces.ColorOKLABa +import org.openrndr.extra.color.spaces.ColorOKLABa import kotlin.reflect.KClass internal fun generateColorTransform(kClass: KClass<*>): String { diff --git a/orx-shade-styles/src/commonMain/kotlin/LinearGradient.kt b/orx-shade-styles/src/commonMain/kotlin/LinearGradient.kt index 64d3cdf5..575741c1 100644 --- a/orx-shade-styles/src/commonMain/kotlin/LinearGradient.kt +++ b/orx-shade-styles/src/commonMain/kotlin/LinearGradient.kt @@ -7,7 +7,7 @@ import org.openrndr.extra.parameters.Description import org.openrndr.extra.parameters.DoubleParameter import org.openrndr.extra.shaderphrases.preprocess import org.openrndr.extras.color.phrases.ColorPhraseBook -import org.openrndr.extras.color.spaces.ColorOKLABa +import org.openrndr.extra.color.spaces.ColorOKLABa import org.openrndr.math.CastableToVector4 import org.openrndr.math.Vector2 import kotlin.reflect.KClass diff --git a/orx-shade-styles/src/commonMain/kotlin/NPointLinearGradient.kt b/orx-shade-styles/src/commonMain/kotlin/NPointLinearGradient.kt index 91b18659..39d5af11 100644 --- a/orx-shade-styles/src/commonMain/kotlin/NPointLinearGradient.kt +++ b/orx-shade-styles/src/commonMain/kotlin/NPointLinearGradient.kt @@ -5,7 +5,7 @@ import org.openrndr.color.ColorRGBa import org.openrndr.color.ConvertibleToColorRGBa import org.openrndr.draw.ShadeStyle import org.openrndr.extra.parameters.Description -import org.openrndr.extras.color.spaces.ColorOKLABa +import org.openrndr.extra.color.spaces.ColorOKLABa import org.openrndr.math.CastableToVector4 import org.openrndr.math.Vector2 diff --git a/orx-shade-styles/src/commonMain/kotlin/RadialGradient.kt b/orx-shade-styles/src/commonMain/kotlin/RadialGradient.kt index 9ef4bcab..e0e6e8ac 100644 --- a/orx-shade-styles/src/commonMain/kotlin/RadialGradient.kt +++ b/orx-shade-styles/src/commonMain/kotlin/RadialGradient.kt @@ -6,7 +6,7 @@ import org.openrndr.draw.shadeStyle import org.openrndr.extra.parameters.ColorParameter import org.openrndr.extra.parameters.Description import org.openrndr.extra.parameters.DoubleParameter -import org.openrndr.extras.color.spaces.ColorOKLABa +import org.openrndr.extra.color.spaces.ColorOKLABa import org.openrndr.math.CastableToVector4 import org.openrndr.math.Vector2 diff --git a/orx-shapes/src/commonMain/kotlin/drawers/BezierPatchDrawer.kt b/orx-shapes/src/commonMain/kotlin/drawers/BezierPatchDrawer.kt index 71151a26..5b1179e8 100644 --- a/orx-shapes/src/commonMain/kotlin/drawers/BezierPatchDrawer.kt +++ b/orx-shapes/src/commonMain/kotlin/drawers/BezierPatchDrawer.kt @@ -13,7 +13,7 @@ import org.openrndr.draw.ShadeStyleGLSL.Companion.vertexMainConstants import org.openrndr.extra.shaderphrases.preprocess import org.openrndr.extra.shapes.phrases.BezierPhraseBook import org.openrndr.extras.color.phrases.ColorPhraseBook -import org.openrndr.extras.color.spaces.ColorOKLABa +import org.openrndr.extra.color.spaces.ColorOKLABa import org.openrndr.math.Vector4 import kotlin.jvm.JvmName