Skip to content

Commit

Permalink
Add wasm target (#41)
Browse files Browse the repository at this point in the history
Kotest doesn't support wasm yet, so I moved the tests to a separate module that doesn't target it.
  • Loading branch information
ajalt authored Jan 25, 2024
1 parent ca4c218 commit 253db8c
Show file tree
Hide file tree
Showing 47 changed files with 98 additions and 60 deletions.
19 changes: 10 additions & 9 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: |
:colormath:jsNodeTest
:colormath:jvmTest
:colormath:linuxX64Test
:test:jsNodeTest
:test:jvmTest
:test:linuxX64Test
:colormath:compileKotlinLinuxArm64
:extensions:colormath-ext-jetpack-compose:check
:extensions:colormath-ext-android-colorint:check
:colormath:compileKotlinWasmJs
:extensions:colormath-ext-jetpack-compose:check
:extensions:colormath-ext-android-colorint:check
:extensions:colormath-ext-android-color:check
--stacktrace
- name: Bundle the build report
Expand All @@ -44,7 +45,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: |
Expand All @@ -63,7 +64,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: mingwX64Test --stacktrace
Expand All @@ -76,7 +77,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- name: Fetch git tags
run: git fetch origin +refs/tags/*:refs/tags/*
- name: Deploy to sonatype
Expand Down
21 changes: 13 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: |
:colormath:jsNodeTest
:colormath:jvmTest
:colormath:linuxX64Test
:test:jsNodeTest
:test:jvmTest
:test:linuxX64Test
:colormath:compileKotlinLinuxArm64
:colormath:compileKotlinWasmJs
:extensions:colormath-ext-jetpack-compose:check
:extensions:colormath-ext-android-colorint:check
:extensions:colormath-ext-android-color:check --stacktrace
:extensions:colormath-ext-android-color:check
--stacktrace
- name: Bundle the build report
if: failure()
run: find . -type d -name 'reports' | zip -@ -r build-reports.zip
Expand All @@ -41,14 +43,17 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: |
macosX64Test
:colormath:compileKotlinMacosArm64
tvosX64Test
iosX64Test
tvosX64Test
iosSimulatorArm64Test
tvosSimulatorArm64Test
watchosSimulatorArm64Test
--stacktrace
windows-tests:
runs-on: windows-latest
Expand All @@ -57,7 +62,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 20
java-version: 21
- uses: gradle/gradle-build-action@v2
with:
arguments: mingwX64Test --stacktrace
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ dependencies {

#### Multiplatform

Colormath supports the following targets: `jvm`, `mingwX64`, `linuxX64`, `macosX64`, `ios`,
`watchos`, `tvos`, and `js` (for both NodeJS and Browsers). You'll need to use Gradle 6 or newer.
Artifacts for `macosArm64` and `linuxArm64` are also published, but not tested with CI.
Colormath publishes artifacts for all
[Tier 1 and Tier 2](https://kotlinlang.org/docs/native-target-support.html)
targets, as well as `mingwX64` and `wasm`.

`wasm` and the `arm64` targets are not tested on CI.

#### Snapshots

Expand Down
4 changes: 3 additions & 1 deletion colormath/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ kotlin {
jvm()
js { nodejs() }

@OptIn(ExperimentalWasmDsl::class)
wasmJs { nodejs() }

linuxX64()
linuxArm64()
mingwX64()
Expand All @@ -32,7 +35,6 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(libs.kotest)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.ajalt.colormath.internal

import kotlin.native.concurrent.SharedImmutable

// Constants used in LAB and LUV conversions.
// http://www.brucelindbloom.com/index.html?LContinuity.html
/** ϵ = (6/29)^3 */
Expand All @@ -15,12 +13,10 @@ internal const val CIE_E_times_K = 8.0

/** The CIECAM02 transform matrix for XYZ -> LMS */
// https://en.wikipedia.org/wiki/CIECAM02#CAT02
@SharedImmutable
internal val CAT02_XYZ_TO_LMS = Matrix(
+0.7328f, +0.4296f, -0.1624f,
-0.7036f, +1.6975f, +0.0061f,
+0.0030f, +0.0136f, +0.9834f,
)

@SharedImmutable
internal val CAT02_LMS_TO_XYZ = CAT02_XYZ_TO_LMS.inverse()
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.ajalt.colormath.ColorSpace
import com.github.ajalt.colormath.internal.*
import com.github.ajalt.colormath.model.RGBColorSpaces.BT2020
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ65
import kotlin.native.concurrent.SharedImmutable

/**
* The ICtCp color space, designed for high dynamic range and wide color gamut imagery.
Expand Down Expand Up @@ -99,45 +98,37 @@ private object PqNonlinearity : RGBColorSpace.TransferFunctions {
}
}

@SharedImmutable
private val ICTCP_RGB_TO_LMS = Matrix(
1688f, 2146f, 262f,
683f, 2951f, 462f,
99f, 309f, 3688f,
).scalarDiv(4096f, inPlace = true)

@SharedImmutable
private val ICTCP_LMS_TO_ICTCP = Matrix(
2048f, 2048f, 0f,
6610f, -13613f, 7003f,
17933f, -17390f, -543f,
).scalarDiv(4096f, inPlace = true)

@SharedImmutable
private val ICTCP_LMS_to_RGB = ICTCP_RGB_TO_LMS.inverse()

@SharedImmutable
private val ICTCP_ICTCP_to_LMS = ICTCP_LMS_TO_ICTCP.inverse()

// ICtCp defines the XYZ to LMS matrix by multiplying a crosstalk matrix with the old
// Hunt-Pointer-Estevez transform. It's not clear why they use HPE rather than one of the newer
// transforms.
@SharedImmutable
private val ICTCP_CROSSTALK = Matrix(
0.92f, 0.04f, 0.04f,
0.04f, 0.92f, 0.04f,
0.04f, 0.04f, 0.92f,
)

@SharedImmutable
private val HPE_XYZ_TO_LMS = Matrix(
0.4002f, 0.7076f, -0.0808f,
-0.2263f, 1.1653f, 0.0457f,
0f, 0f, 0.9182f,
)

@SharedImmutable
private val ICTCP_XYZ_TO_LMS = ICTCP_CROSSTALK.dot(HPE_XYZ_TO_LMS)

@SharedImmutable
private val ICTCP_LMS_TO_XYZ = ICTCP_XYZ_TO_LMS.inverse()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.github.ajalt.colormath.WhitePoint
import com.github.ajalt.colormath.internal.*
import kotlin.math.log2
import kotlin.math.pow
import kotlin.native.concurrent.SharedImmutable

object RGBColorSpaces {
/**
Expand Down Expand Up @@ -264,13 +263,10 @@ private data class RGBColorSpaceImpl(
override operator fun invoke(r: Float, g: Float, b: Float, alpha: Float): RGB = RGB(r, g, b, alpha, this)
}

@SharedImmutable
private val SRGB_R = xyY(0.6400, 0.3300)

@SharedImmutable
private val SRGB_G = xyY(0.3000, 0.6000)

@SharedImmutable
private val SRGB_B = xyY(0.1500, 0.0600)

private object SRGBTransferFunctions : RGBColorSpace.TransferFunctions {
Expand All @@ -289,27 +285,20 @@ private object SRGBTransferFunctions : RGBColorSpace.TransferFunctions {
}
}

@SharedImmutable
private val ACES_WHITE_POINT = WhitePoint("ACES", xyY(0.32168, 0.33767))

// values from [Academy TB-2014-004]
@SharedImmutable
private val ACES_AP0_R = xyY(0.73470, 0.26530)

@SharedImmutable
private val ACES_AP0_G = xyY(0.00000, 1.00000)

@SharedImmutable
private val ACES_AP0_B = xyY(0.00010, -0.0770)

// values from [Academy S-2014-004]
@SharedImmutable
private val ACES_AP1_R = xyY(0.713, 0.293)

@SharedImmutable
private val ACES_AP1_G = xyY(0.165, 0.830)

@SharedImmutable
private val ACES_AP1_B = xyY(0.128, 0.044)

// from [Academy S-2014-003]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.github.ajalt.colormath.internal.dot
import com.github.ajalt.colormath.model.*
import com.github.ajalt.colormath.model.RGBColorSpaces.SRGB
import com.github.ajalt.colormath.model.XYZColorSpaces.XYZ65
import kotlin.native.concurrent.SharedImmutable

/**
* Create a chromatic adapter that will adapt colors from a given [sourceWhite] to this color space's
Expand Down Expand Up @@ -69,8 +68,6 @@ private inline fun <T> doAdapt(transform: Matrix, r: Float, g: Float, b: Float,
}
}

@SharedImmutable
private val xyzToSrgb = Matrix(SRGB.matrixFromXyz)

@SharedImmutable
private val srgbToXYZ = Matrix(SRGB.matrixToXyz)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.github.ajalt.colormath.ColorComponentInfo
import com.github.ajalt.colormath.ColorSpace
import com.github.ajalt.colormath.internal.scaleRange
import com.github.ajalt.colormath.transform.InterpolationMethod.Point
import kotlin.native.concurrent.SharedImmutable

/**
* Interpolate linearly between this color and [other].
Expand Down Expand Up @@ -373,7 +372,6 @@ private fun requireComponentName(space: ColorSpace<*>, name: String): String {
return name.lowercase()
}

@SharedImmutable
private val alphaAdjustment: ComponentAdjustment = { l ->
when {
l.all { it.isNaN() } -> l
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.ajalt.colormath.internal

import io.kotest.matchers.shouldBe
import kotlin.js.JsName
import kotlin.test.Test
import kotlin.test.assertEquals

class MatrixTest {
@Test
Expand Down Expand Up @@ -56,3 +56,10 @@ class MatrixTest {
l.dot(10f, 20f, 30f).values shouldBe floatArrayOf(140f, 320f, 500f)
}
}

// TODO(kotest): go back to kotest once is supports wasm
private infix fun FloatArray.shouldBe(other: FloatArray) {
for (i in indices) {
assertEquals(this[i], other[i], 0.00000001f, "index $i")
}
}
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[versions]
kotlin = "1.9.20"
jbCompose = "1.5.10"
kotlin = "1.9.21"
jbCompose = "1.5.11"

[libraries]
# used in tests
kotest = "io.kotest:kotest-assertions-core:5.7.2"
kotest = "io.kotest:kotest-assertions-core:5.8.0"
compose-ui-graphics = { module = "org.jetbrains.compose.ui:ui-graphics", version.ref = "jbCompose" }
androidx-annotation = "androidx.annotation:annotation:1.7.0"
junit = "junit:junit:4.13.2"
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include("colormath")
include("test")
include("extensions:colormath-ext-android-color")
include("extensions:colormath-ext-android-colorint")
include("extensions:colormath-ext-jetpack-compose")
Expand Down
46 changes: 46 additions & 0 deletions test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
kotlin("multiplatform")
alias(libs.plugins.publish)
}

repositories {
mavenCentral()
}

kotlin {
jvm()
js { nodejs() }

linuxX64()
linuxArm64()
mingwX64()
macosX64()
macosArm64()
iosX64()
iosArm64()
iosSimulatorArm64()
tvosX64()
tvosArm64()
tvosSimulatorArm64()
watchosX64()
watchosArm64()
watchosSimulatorArm64()

sourceSets {
val commonTest by getting {
dependencies {
api(project(":colormath"))
implementation(libs.kotest)
implementation(kotlin("test"))
}
}
}
}

tasks.withType<Jar>().configureEach {
manifest {
attributes("Automatic-Module-Name" to "com.github.ajalt.colormath")
}
}
2 changes: 2 additions & 0 deletions test/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POM_ARTIFACT_ID=colormath
POM_NAME=Colormath
Loading

0 comments on commit 253db8c

Please sign in to comment.