From 096cb2367a98ed7d976072a5c15b8adee47bf451 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 10 Dec 2023 23:03:41 +0100 Subject: [PATCH] [orx-color] Match new LuminosityColor --- .../commonMain/kotlin/spaces/ColorHPLUVa.kt | 11 +++++++++-- .../commonMain/kotlin/spaces/ColorHSLUVa.kt | 12 ++++++++++-- .../commonMain/kotlin/spaces/ColorOKHSLa.kt | 6 ++++-- .../commonMain/kotlin/spaces/ColorOKHSVa.kt | 6 ++++-- .../commonMain/kotlin/spaces/ColorOKLABa.kt | 5 +++++ .../commonMain/kotlin/spaces/ColorOKLCHa.kt | 11 +++++++++-- .../commonMain/kotlin/spaces/ColorXSLUVa.kt | 6 ++++-- .../kotlin/tools/ColorRGBaExtensions.kt | 19 +++++++++++++++++++ .../src/jvmDemo/kotlin/DemoColorPalette02.kt | 3 ++- 9 files changed, 66 insertions(+), 13 deletions(-) diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorHPLUVa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorHPLUVa.kt index 292a489b..3d3eb314 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorHPLUVa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorHPLUVa.kt @@ -11,6 +11,7 @@ data class ColorHPLUVa(val h: Double, val s: Double, val l: Double, override val HueShiftableColor, SaturatableColor, ShadableColor, + LuminosityColor, AlgebraicColor { fun toLCHUVa(): ColorLCHUVa { val l1 = l @@ -26,13 +27,15 @@ data class ColorHPLUVa(val h: Double, val s: Double, val l: Double, override val return ColorLCHUVa(l100, c100, h) } - override val hue: Double = h + override val hue: Double + get() = h override fun withHue(hue: Double) = copy(h = hue) override fun shade(factor: Double): ColorHPLUVa = copy(l = l * factor) - override val saturation: Double = s + override val saturation: Double + get() = s override fun withSaturation(saturation: Double): ColorHPLUVa = copy(s = saturation) @@ -49,6 +52,10 @@ data class ColorHPLUVa(val h: Double, val s: Double, val l: Double, override val override fun mix(other: ColorHPLUVa, factor: Double) = mix(this, other, factor) override fun toVector4(): Vector4 = Vector4(h, s, l, alpha) + override val luminosity: Double + get() = l + + override fun withLuminosity(luminosity: Double): ColorHPLUVa = copy(l = luminosity) } fun mix(left: ColorHPLUVa, right: ColorHPLUVa, x: Double): ColorHPLUVa { diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorHSLUVa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorHSLUVa.kt index 3afc9caf..0c9ec79b 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorHSLUVa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorHSLUVa.kt @@ -86,6 +86,7 @@ data class ColorHSLUVa(val h: Double, val s: Double, val l: Double, override val HueShiftableColor, SaturatableColor, ShadableColor, + LuminosityColor, AlgebraicColor { @Deprecated("Legacy alpha parameter name", ReplaceWith("alpha")) @@ -114,11 +115,14 @@ data class ColorHSLUVa(val h: Double, val s: Double, val l: Double, override val return ColorXSLUVa(hueToX(h), s, l, alpha) } - override val hue: Double =h + override val hue: Double + get() = h override fun withHue(hue: Double): ColorHSLUVa = copy(h = hue) override fun shade(factor: Double) = copy(l = l * factor) - override val saturation: Double = s + override val saturation: Double + get() = s + override fun withSaturation(saturation: Double): ColorHSLUVa = copy(s = saturation) override fun toRGBa(): ColorRGBa { @@ -136,6 +140,10 @@ data class ColorHSLUVa(val h: Double, val s: Double, val l: Double, override val override fun mix(other: ColorHSLUVa, factor: Double) = mix(this, other, factor) override fun toVector4(): Vector4 = Vector4(h, s, l, alpha) + override val luminosity: Double + get() = l + + override fun withLuminosity(luminosity: Double): ColorHSLUVa = copy(l = luminosity) } diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSLa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSLa.kt index 3cdd10a7..6107b41f 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSLa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSLa.kt @@ -95,12 +95,14 @@ data class ColorOKHSLa(val h: Double, val s: Double, val l: Double, override val ).toRGBa().toSRGB() } - override val hue: Double = h + override val hue: Double + get() = h override fun withHue(hue: Double): ColorOKHSLa = copy(h = hue) override fun opacify(factor: Double): ColorOKHSLa = copy(alpha = alpha * factor) - override val saturation: Double = s + override val saturation: Double + get() = s override fun withSaturation(saturation: Double): ColorOKHSLa = copy(s = saturation) diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSVa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSVa.kt index 2fd5cb18..9a8b6373 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKHSVa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKHSVa.kt @@ -102,12 +102,14 @@ data class ColorOKHSVa(val h: Double, val s: Double, val v: Double, override val ).toRGBa().toSRGB() } - override val hue: Double = h + override val hue: Double + get() = h override fun withHue(hue: Double): ColorOKHSVa = copy(h = hue) override fun opacify(factor: Double): ColorOKHSVa = copy(alpha = alpha * factor) - override val saturation: Double = s + override val saturation: Double + get() = s override fun withSaturation(saturation: Double): ColorOKHSVa = copy(s = saturation) override fun shade(factor: Double): ColorOKHSVa = copy(v = v * factor) diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKLABa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKLABa.kt index b1d1604e..225bf8c0 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKLABa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKLABa.kt @@ -16,6 +16,7 @@ import kotlin.math.pow data class ColorOKLABa(val l: Double, val a: Double, val b: Double, override val alpha: Double = 1.0) : ColorModel, ShadableColor, + LuminosityColor, AlgebraicColor { companion object { @@ -66,6 +67,10 @@ data class ColorOKLABa(val l: Double, val a: Double, val b: Double, override val override fun times(scale: Double) = ColorOKLABa(l * scale, a * scale, b * scale, alpha * scale) override fun toVector4() = Vector4(l, a, b, alpha) + override val luminosity: Double + get() = l + + override fun withLuminosity(luminosity: Double): ColorOKLABa = copy(l = luminosity) } fun ColorRGBa.toOKLABa() = ColorOKLABa.fromRGBa(this) \ No newline at end of file diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorOKLCHa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorOKLCHa.kt index 008f4be9..8d47541d 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorOKLCHa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorOKLCHa.kt @@ -14,6 +14,7 @@ data class ColorOKLCHa(val l: Double, val c: Double, val h: Double, override val ShadableColor, ChromaColor, HueShiftableColor, + LuminosityColor, AlgebraicColor { companion object { @@ -49,11 +50,17 @@ data class ColorOKLCHa(val l: Double, val c: Double, val h: Double, override val override fun toRGBa(): ColorRGBa = toOKLABa().toRGBa() override fun toVector4(): Vector4 = Vector4(l, c, h, alpha) - override val chroma: Double = c + override val chroma: Double + get() = c override fun withChroma(chroma: Double): ColorOKLCHa = copy(c = chroma) - override val hue: Double = h + override val hue: Double + get() = h override fun withHue(hue: Double): ColorOKLCHa = copy(h = hue) + override val luminosity: Double + get() = l + + override fun withLuminosity(luminosity: Double): ColorOKLCHa = copy(l = luminosity) } fun mix(left: ColorOKLCHa, right: ColorOKLCHa, x: Double): ColorOKLCHa { diff --git a/orx-color/src/commonMain/kotlin/spaces/ColorXSLUVa.kt b/orx-color/src/commonMain/kotlin/spaces/ColorXSLUVa.kt index f8d850f7..b10a0773 100644 --- a/orx-color/src/commonMain/kotlin/spaces/ColorXSLUVa.kt +++ b/orx-color/src/commonMain/kotlin/spaces/ColorXSLUVa.kt @@ -16,11 +16,13 @@ data class ColorXSLUVa(val x: Double, val s: Double, val l: Double, override val @Deprecated("Legacy alpha parameter name", ReplaceWith("alpha")) val a = alpha - override val hue: Double = x + override val hue: Double + get() = x override fun withHue(hue: Double): ColorXSLUVa = copy(x = hue) override fun shade(factor: Double) = copy(l = l * factor) - override val saturation: Double = s + override val saturation: Double + get() = s override fun withSaturation(saturation: Double): ColorXSLUVa = copy(s = saturation) diff --git a/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt b/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt index ed6a0a05..31c7184b 100644 --- a/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt +++ b/orx-color/src/commonMain/kotlin/tools/ColorRGBaExtensions.kt @@ -57,6 +57,25 @@ inline fun > ColorRGBa.convertTo(): T { return converted as T } +inline fun ColorRGBa.withLuminosity(luminosity: Double): ColorRGBa + where T : LuminosityColor, + T : ColorModel, + T : ConvertibleToColorRGBa = convertTo().withLuminosity(luminosity).toRGBa().matchLinearity(this) + +inline fun ColorRGBa.mixLuminosity(luminosity: Double, factor: Double): ColorRGBa + where T : LuminosityColor, + T : ColorModel, + T : ConvertibleToColorRGBa = + convertTo().mixLuminosity(luminosity, factor).toRGBa().matchLinearity(this) + + +inline fun ColorRGBa.shadeLuminosity(factor: Double): ColorRGBa + where T : LuminosityColor, + T : ColorModel, + T : ConvertibleToColorRGBa = + convertTo().shadeLuminosity(factor).toRGBa().matchLinearity(this) + + inline fun ColorRGBa.mixHue(hue: Double, factor: Double): ColorRGBa where T : HueShiftableColor, T : ColorModel, diff --git a/orx-color/src/jvmDemo/kotlin/DemoColorPalette02.kt b/orx-color/src/jvmDemo/kotlin/DemoColorPalette02.kt index d9eb2b0a..6660ada5 100644 --- a/orx-color/src/jvmDemo/kotlin/DemoColorPalette02.kt +++ b/orx-color/src/jvmDemo/kotlin/DemoColorPalette02.kt @@ -2,6 +2,7 @@ import org.openrndr.application import org.openrndr.extra.color.palettes.analogous import org.openrndr.extra.color.spaces.HSLuv import org.openrndr.extra.color.spaces.RGB +import org.openrndr.extra.color.tools.shadeLuminosity /** * By default, generated palettes contain colors of varying hue @@ -13,7 +14,7 @@ fun main() = application { program { val count = 8 val palette = RGB.PINK.analogous(360.0, count).mapIndexed { i, c -> - c.shade((i + 1.0) / count) + c.shadeLuminosity((i + 1.0) / count) }.reversed() extend { drawer.stroke = null