Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ permissions:
contents: read

jobs:
test:
name: Tests
jvm:
name: JVM And Lint
runs-on: ubuntu-latest

steps:
Expand All @@ -36,3 +36,37 @@ jobs:

- name: Check formatting
run: ./gradlew :phosphor-core:ktlintCheck

ios_compile:
name: iOS Compile
runs-on: macos-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'zulu'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Compile iOS target
run: ./gradlew :phosphor-core:compileKotlinIosArm64

test:
name: Tests
runs-on: ubuntu-latest
needs: [jvm, ios_compile]
if: ${{ always() }}

steps:
- name: Enforce required checks
run: |
if [ "${{ needs.jvm.result }}" != "success" ] || [ "${{ needs.ios_compile.result }}" != "success" ]; then
echo "Required CI jobs did not all pass."
exit 1
fi
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
uses: gradle/actions/setup-gradle@v4

- name: Run tests before publish
run: ./gradlew :phosphor-core:jvmTest
run: ./gradlew :phosphor-core:jvmTest :phosphor-core:compileKotlinIosArm64

- name: Decode signing key
if: ${{ github.event.inputs.dry_run != 'true' }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package link.socket.phosphor.color

import kotlin.jvm.JvmInline
import kotlin.math.abs
import kotlin.math.roundToInt

Expand Down Expand Up @@ -29,9 +30,9 @@ value class NeutralColor private constructor(
val alphaInt: Int get() = (packedRgba and CHANNEL_MASK).toInt()

fun toHex(includeAlpha: Boolean = true): String {
val rgb = "%02X%02X%02X".format(redInt, greenInt, blueInt)
val rgb = channelToHex(redInt) + channelToHex(greenInt) + channelToHex(blueInt)
return if (includeAlpha) {
"#$rgb%02X".format(alphaInt)
"#$rgb${channelToHex(alphaInt)}"
} else {
"#$rgb"
}
Expand Down Expand Up @@ -155,5 +156,7 @@ value class NeutralColor private constructor(
): Float = start + ((end - start) * t)

private fun toChannel(value: Float): Int = (value.coerceIn(0f, 1f) * CHANNEL_MAX_FLOAT).roundToInt()

private fun channelToHex(channel: Int): String = channel.toString(radix = 16).uppercase().padStart(2, '0')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class NeutralColorTest {
assertEquals("#AABBCC", color.toHex(includeAlpha = false))
}

@Test
fun `toHex pads channels with leading zeros`() {
val color = NeutralColor.fromHex("#010A0B0C")

assertEquals("#010A0B0C", color.toHex())
}

@Test
fun `fromRgba clamps out of range channels`() {
val color = NeutralColor.fromRgba(-1f, 0.5f, 2f, 9f)
Expand Down