How does iOS go about calculating a toolbar gradient? I’d like to be able to set my window’s background color to match the end color in the gradient so that it appears to fade into the view. The problem is that the end tint seems to vary depending on the base color and I can’t find a consistent formula that produces a match.
Share
My best approximation:
How I derived that:
Let
c = (r, g, b) = base color.Let
c' = (r', g', b') = resulting color.I let
c = #944321 = (148, 67, 33)and gotc' = #84361b = (132, 54, 27).That gives me three equations to solve:
Since we have three equations, let
f(x)be a polynomial with three unknowns:From there I made a 3×4 matrix on my calculator, inputted the different values of x, and performed a reduced-row-echelon on the matrix, the solution being
(0.001468, 0.647295, 4.04025). I plug those into A, B and C and got the aforementionedf(x).Here’s a quick Python script to perform the calculation.
Usage:
./script.py rrggbbI tested this with the color
#9e3393and got#8f2983, same color iOS produced:Even if it’s not exact sometimes it will probably be impossible to tell since you’re using the color at the end of a gradient. Hope that helps!