Skip to content

Commit

Permalink
Make all color class constructors public
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Jan 4, 2025
1 parent d7ce6f8 commit 25de593
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 41 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased
### Changed
- `HSV.toSRGB()` with NaN a hue value now returns a greyscale `RGB` color instead of a NaN color. ([#67](https://github.com/ajalt/colormath/issues/67))
- All color class constructors are now public to avoid warning when using `copy()` ([#65](https://github.com/ajalt/colormath/issues/65))
- `HSV.toSRGB()` with NaN a hue value now returns a greyscale `RGB` color instead of a NaN color. ([#67](https://github.com/ajalt/colormath/issues/67))
- Update Kotlin to 2.1.0

## 3.6.0
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ apiValidation {
project("scripts").subprojects.mapTo(ignoredProjects) { it.name }
project("test").subprojects.mapTo(ignoredProjects) { it.name }
ignoredProjects.add("website")
ignoredProjects.add("test")
}


Expand Down
6 changes: 6 additions & 0 deletions colormath/api/colormath.api
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ public final class com/github/ajalt/colormath/model/JzCzHz$Companion : com/githu

public final class com/github/ajalt/colormath/model/LAB : com/github/ajalt/colormath/Color {
public static final field Companion Lcom/github/ajalt/colormath/model/LAB$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/LABColorSpace;)V
public synthetic fun clamp ()Lcom/github/ajalt/colormath/Color;
public fun clamp ()Lcom/github/ajalt/colormath/model/LAB;
public final fun component1 ()F
Expand Down Expand Up @@ -846,6 +847,7 @@ public final class com/github/ajalt/colormath/model/LABKt {

public final class com/github/ajalt/colormath/model/LCHab : com/github/ajalt/colormath/HueColor {
public static final field Companion Lcom/github/ajalt/colormath/model/LCHab$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/LCHabColorSpace;)V
public synthetic fun clamp ()Lcom/github/ajalt/colormath/Color;
public fun clamp ()Lcom/github/ajalt/colormath/model/LCHab;
public final fun component1 ()F
Expand Down Expand Up @@ -923,6 +925,7 @@ public final class com/github/ajalt/colormath/model/LCHabKt {

public final class com/github/ajalt/colormath/model/LCHuv : com/github/ajalt/colormath/HueColor {
public static final field Companion Lcom/github/ajalt/colormath/model/LCHuv$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/LCHuvColorSpace;)V
public synthetic fun clamp ()Lcom/github/ajalt/colormath/Color;
public fun clamp ()Lcom/github/ajalt/colormath/model/LCHuv;
public final fun component1 ()F
Expand Down Expand Up @@ -1000,6 +1003,7 @@ public final class com/github/ajalt/colormath/model/LCHuvKt {

public final class com/github/ajalt/colormath/model/LUV : com/github/ajalt/colormath/Color {
public static final field Companion Lcom/github/ajalt/colormath/model/LUV$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/LUVColorSpace;)V
public synthetic fun clamp ()Lcom/github/ajalt/colormath/Color;
public fun clamp ()Lcom/github/ajalt/colormath/model/LUV;
public final fun component1 ()F
Expand Down Expand Up @@ -1183,6 +1187,7 @@ public final class com/github/ajalt/colormath/model/Oklch$Companion : com/github

public final class com/github/ajalt/colormath/model/RGB : com/github/ajalt/colormath/Color {
public static final field Companion Lcom/github/ajalt/colormath/model/RGB$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/RGBColorSpace;)V
public synthetic fun clamp ()Lcom/github/ajalt/colormath/Color;
public fun clamp ()Lcom/github/ajalt/colormath/model/RGB;
public final fun component1 ()F
Expand Down Expand Up @@ -1431,6 +1436,7 @@ public final class com/github/ajalt/colormath/model/SRGB : com/github/ajalt/colo

public final class com/github/ajalt/colormath/model/XYZ : com/github/ajalt/colormath/Color {
public static final field Companion Lcom/github/ajalt/colormath/model/XYZ$Companion;
public fun <init> (FFFFLcom/github/ajalt/colormath/model/XYZColorSpace;)V
public final fun adaptTo (Lcom/github/ajalt/colormath/model/XYZColorSpace;)Lcom/github/ajalt/colormath/model/XYZ;
public final fun adaptTo (Lcom/github/ajalt/colormath/model/XYZColorSpace;[F)Lcom/github/ajalt/colormath/model/XYZ;
public final fun adaptTo (Lcom/github/ajalt/colormath/model/XYZColorSpace;[F[F)Lcom/github/ajalt/colormath/model/XYZ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.github.ajalt.colormath.model.RGBColorSpaces.ROMM_RGB
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ50
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ65
import kotlin.jvm.JvmOverloads
import kotlin.math.roundToInt

/**
* Parse a string representing a CSS color value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal fun Float.spow(p: Double): Double = toDouble().spow(p)

internal fun sqrtSumSq(a: Float, b: Float, c: Float): Float = sqrt(a.pow(2) + b.pow(2) + c.pow(2))
internal fun sqrtSumSq(a: Double, b: Double): Double = sqrt(a.pow(2) + b.pow(2))
internal fun sqrtSumSq(a: Double, b: Double, c: Double): Double = sqrt(a.pow(2) + b.pow(2) + c.pow(2))
internal fun sqrtSumSq(a: Double, b: Double, c: Double): Double =
sqrt(a.pow(2) + b.pow(2) + c.pow(2))

internal fun scaleRange(l1: Float, r1: Float, l2: Float, r2: Float, t: Float): Float {
return if (r1 == l1) t else (r2 - l2) * (t - l1) / (r1 - l1) + l2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ internal value class Matrix(val rowMajor: FloatArray) {
v00: Float, v10: Float, v20: Float,
v01: Float, v11: Float, v21: Float,
v02: Float, v12: Float, v22: Float,
) : this(floatArrayOf(
v00, v10, v20,
v01, v11, v21,
v02, v12, v22,
))
) : this(
floatArrayOf(
v00, v10, v20,
v01, v11, v21,
v02, v12, v22,
)
)

fun copy() = Matrix(rowMajor.copyOf())

Expand Down Expand Up @@ -66,7 +68,12 @@ internal fun Matrix.inverse(inPlace: Boolean = false): Matrix {
return out
}

internal inline fun <T> Matrix.dot(v0: Float, v1: Float, v2: Float, block: (Float, Float, Float) -> T): T {
internal inline fun <T> Matrix.dot(
v0: Float,
v1: Float,
v2: Float,
block: (Float, Float, Float) -> T,
): T {
return block(
get(0, 0) * v0 + get(1, 0) * v1 + get(2, 0) * v2,
get(0, 1) * v0 + get(1, 1) * v1 + get(2, 1) * v2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object LABColorSpaces {
* | a* | green-red | `[-128, 128]` |
* | b* | blue-yellow | `[-128, 128]` |
*/
data class LAB internal constructor(
data class LAB(
val l: Float,
val a: Float,
val b: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object LCHabColorSpaces {
* | c | chroma | `[0, 150]` |
* | h | hue, degrees, `NaN` for monochrome colors | `[0, 360)` |
*/
data class LCHab internal constructor(
data class LCHab(
val l: Float,
val c: Float,
override val h: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object LCHuvColorSpaces {
* | [c] | chroma | `[0, 100]` |
* | [h] | hue, degrees, `NaN` for monochrome colors | `[0, 360)` |
*/
data class LCHuv internal constructor(
data class LCHuv(
val l: Float,
val c: Float,
override val h: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object LUVColorSpaces {
* | [u] | | `[-100, 100]` |
* | [v] | | `[-100, 100]` |
*/
data class LUV internal constructor(
data class LUV(
val l: Float,
val u: Float,
val v: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.github.ajalt.colormath.Color
import com.github.ajalt.colormath.ColorComponentInfo
import com.github.ajalt.colormath.ColorSpace
import com.github.ajalt.colormath.HueColor
import com.github.ajalt.colormath.internal.*
import com.github.ajalt.colormath.internal.clampTrailingHue
import com.github.ajalt.colormath.internal.componentInfoList
import com.github.ajalt.colormath.internal.doCreate
import com.github.ajalt.colormath.internal.fromPolarModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ interface RGBColorSpace : WhitePointColorSpace<RGB> {
* | [g] | green | `[0, 1]` |
* | [b] | blue | `[0, 1]` |
*/
data class RGB internal constructor(
data class RGB(
val r: Float,
val g: Float,
val b: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object XYZColorSpaces {
* | [y] | `[0, 1]` |
* | [z] | `[0, 1]` |
*/
data class XYZ internal constructor(
data class XYZ(
val x: Float,
val y: Float,
val z: Float,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ fun RGBInt.Companion.createChromaticAdapter(sourceWhite: xyY): ChromaticAdapterR
return ChromaticAdapterRGBInt(xyzToSrgb.dot(xyzTransform).dot(srgbToXYZ))
}

class ChromaticAdapterRGB internal constructor(private val space: RGBColorSpace, private val transform: Matrix) {
class ChromaticAdapterRGB internal constructor(
private val space: RGBColorSpace,
private val transform: Matrix,
) {
/** Adapt an sRGB [color] to this white point */
fun adapt(color: RGB): RGB {
return doAdapt(transform, color.r, color.g, color.b) { r, g, b ->
Expand All @@ -61,7 +64,13 @@ class ChromaticAdapterRGBInt internal constructor(private val transform: Matrix)
}
}

private inline fun <T> doAdapt(transform: Matrix, r: Float, g: Float, b: Float, block: (Float, Float, Float) -> T): T {
private inline fun <T> doAdapt(
transform: Matrix,
r: Float,
g: Float,
b: Float,
block: (Float, Float, Float) -> T,
): T {
val f = SRGB.transferFunctions
return transform.dot(f.eotf(r), f.eotf(g), f.eotf(b)) { rr, gg, bb ->
block(f.oetf(rr), f.oetf(gg), f.oetf(bb))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,15 @@ private class CubicBezierEasing(
else -> 0.0
}
}

x > 1 -> {
when {
x2 < 1 -> tangent(x2, y2, 1.0, 1.0, x)
x1 < 1 -> tangent(x1, y1, 1.0, 1.0, x)
else -> 1.0
}
}

else -> sampleCurveY(findTFromX(x))
}.toFloat()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ object HueAdjustments {
val specified: ComponentAdjustment = { it }
}

private inline fun deltaAdjustment(crossinline adj: (delta: Float) -> Float): ComponentAdjustment = { hues ->
hues.toMutableList().also { h ->
h[0] = h[0].normalizeDeg()
for (i in 1..h.lastIndex) {
val hue = h[i]
val prev = h[i - 1]
if (hue.isNaN() || prev.isNaN()) continue
h[i] = prev + adj(hue.normalizeDeg() - prev.normalizeDeg())
private inline fun deltaAdjustment(crossinline adj: (delta: Float) -> Float): ComponentAdjustment =
{ hues ->
hues.toMutableList().also { h ->
h[0] = h[0].normalizeDeg()
for (i in 1..h.lastIndex) {
val hue = h[i]
val prev = h[i - 1]
if (hue.isNaN() || prev.isNaN()) continue
h[i] = prev + adj(hue.normalizeDeg() - prev.normalizeDeg())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ object InterpolationMethods {
* Linear piecewise interpolation
*/
fun linear(): InterpolationMethod = object : InterpolationMethod {
override fun build(points: List<Point>): InterpolationMethod.ComponentInterpolator = LinearInterpolator(points)
override fun build(points: List<Point>): InterpolationMethod.ComponentInterpolator =
LinearInterpolator(points)
}

/**
Expand All @@ -52,16 +53,18 @@ object InterpolationMethods {
* unique parabola passing through them. By default, the boundaries are calculated by one-sided
* finite differences.
*/
fun monotoneSpline(parabolicEndpoints: Boolean = false): InterpolationMethod = object : InterpolationMethod {
override fun build(points: List<Point>): InterpolationMethod.ComponentInterpolator {
return if (points.size < 3) linear().build(points)
else MonotonicSplineInterpolator(points, parabolicEndpoints)
fun monotoneSpline(parabolicEndpoints: Boolean = false): InterpolationMethod =
object : InterpolationMethod {
override fun build(points: List<Point>): InterpolationMethod.ComponentInterpolator {
return if (points.size < 3) linear().build(points)
else MonotonicSplineInterpolator(points, parabolicEndpoints)
}
}
}
}


private class LinearInterpolator(private val points: List<Point>) : InterpolationMethod.ComponentInterpolator {
private class LinearInterpolator(private val points: List<Point>) :
InterpolationMethod.ComponentInterpolator {
init {
require(points.size > 1) { "At least two points are required for interpolation" }
}
Expand Down Expand Up @@ -111,17 +114,21 @@ private class MonotonicSplineInterpolator(
abs(p[0]) > 2 * abs(s[0]) -> 2 * s[0]
else -> p[0]
}

else -> s[0]
}

n -> when {
parabolicEndpoints -> when {
p[n] * s[n - 1] <= 0 -> 0f
abs(p[n]) > 2 * abs(s[n - 1]) -> 2 * s[n - 1]
else -> p[n]

}

else -> s[n - 1]
}

else -> (sign(s[i - 1]) + sign(s[i])) * minOf(abs(s[i - 1]), abs(s[i]), abs(p[i]) / 2)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ fun <T : Color> ColorSpace<T>.mix(
val sum = amount1.toFloat() + amount2.toFloat()
require(sum != 0f) { "mix amounts cannot sum to 0" }
val c = convert(color1).interpolate(color2, amount2.toFloat() / sum, true, hueAdjustment)
return if (sum < 1f) c.map { comps -> comps.also { it[it.lastIndex] = it.last().nanToOne() * sum } } else c
return if (sum < 1f) c.map { comps ->
comps.also {
it[it.lastIndex] = it.last().nanToOne() * sum
}
} else c
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ internal fun rgbToRgbMatrix(src: RGBColorSpace, dst: RGBColorSpace): Matrix {
return if (src.whitePoint == dst.whitePoint) {
Matrix(dst.matrixFromXyz).dot(Matrix(src.matrixToXyz))
} else {
val adaptation = XYZColorSpace(dst.whitePoint).chromaticAdaptationMatrix(src.whitePoint.chromaticity)
val adaptation =
XYZColorSpace(dst.whitePoint).chromaticAdaptationMatrix(src.whitePoint.chromaticity)
Matrix(dst.matrixFromXyz).dot(adaptation).dot(Matrix(src.matrixToXyz))
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview"
[plugins]
dokka = "org.jetbrains.dokka:2.0.0"
publish = "com.vanniktech.maven.publish:0.30.0"
kotlinBinaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:0.14.0"
kotlinBinaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:0.17.0"

# used in extensions
android-library = "com.android.library:8.7.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.github.ajalt.colormath.model

import com.github.ajalt.colormath.companionTest
import com.github.ajalt.colormath.convertToSpaceTest
import com.github.ajalt.colormath.*
import com.github.ajalt.colormath.model.LABColorSpaces.LAB50
import com.github.ajalt.colormath.model.LABColorSpaces.LAB65
import com.github.ajalt.colormath.model.LCHabColorSpaces.LCHab50
import com.github.ajalt.colormath.model.LCHabColorSpaces.LCHab65
import com.github.ajalt.colormath.roundtripTest
import com.github.ajalt.colormath.testColorConversions
import kotlin.js.JsName
import kotlin.test.Test

Expand Down

0 comments on commit 25de593

Please sign in to comment.