Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remap ColorMatrix #739

Merged
merged 3 commits into from
Aug 9, 2023
Merged

Remap ColorMatrix #739

merged 3 commits into from
Aug 9, 2023

Conversation

elijah-semyonov
Copy link

@elijah-semyonov elijah-semyonov commented Aug 8, 2023

Proposed Changes

Remap 4th column of ColorMatrix to from 0..255 to 0..1 range before passing to skiko.

Testing

Test: GraphicsLayerTest.correctColorMatrix

Issues Fixed

Fixes: JetBrains/compose-multiplatform#3461

internal actual fun actualColorMatrixColorFilter(colorMatrix: ColorMatrix): ColorFilter {
// as per https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ColorMatrix
// columns 0, 1, 2, 3 are raw coefficients and 4th column is offset in 0..255 range
// skia expects offset in 0..1 range (https://fiddle.skia.org/c/2b8d12a11e9d22b0d1b85e5b0179cd4b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fiddle example doesn't look like valid proof

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it like there - unwrap loop and use multiply instead of division

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with unrolling loop, but multiply on (1.0 / 255.0) - why? And considering proof, you can just type in the numbers in fiddle and see the result

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just type in the numbers in fiddle and see the result

Let's put official docs/spec instead. Or link to actual android implementation at least.

but multiply on (1.0 / 255.0) - why?

I had two reasons here: repeat original implementation and flashbacks from low-level C world. Since here we're inside managed env, I don't think that it's really matter. The old rule in my head is "multiplication usually faster than division, never slower". Again, it's not really matter here, not a blocker at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ColorMatrix documentation in code itself describes the expected values, and generated doc is linked in the beginning of function:

/**
* 4x5 matrix for transforming the color and alpha components of a source.
* The matrix can be passed as single array, and is treated as follows:
*
* ```
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
* p, q, r, s, t ]
* ```
*
* When applied to a color <code>[[R, G, B, A]]</code>, the resulting color
* is computed as:
*
* ```
* R' = a*R + b*G + c*B + d*A + e;
* G' = f*R + g*G + h*B + i*A + j;
* B' = k*R + l*G + m*B + n*A + o;
* A' = p*R + q*G + r*B + s*A + t;</pre>
*
* ```
* That resulting color <code>[[R', G', B', A']]</code>
* then has each channel clamped to the <code>0</code> to <code>255</code>
* range.
*
* The sample ColorMatrix below inverts incoming colors by scaling each
* channel by <code>-1</code>, and then shifting the result up by
* `255` to remain in the standard color space.
*
* ```
* [ -1, 0, 0, 0, 255,
* 0, -1, 0, 0, 255,
* 0, 0, -1, 0, 255,
* 0, 0, 0, 1, 0 ]
* ```
*
* This is often used as input for [ColorFilter.colorMatrix] and applied at draw time
* through [Paint.colorFilter]
*/

@elijah-semyonov elijah-semyonov marked this pull request as ready for review August 9, 2023 09:29
@elijah-semyonov elijah-semyonov merged commit 01a9d91 into jb-main Aug 9, 2023
1 of 3 checks passed
@elijah-semyonov elijah-semyonov deleted the es/wrong-color-matrix branch August 9, 2023 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants