java - Using color and color.darker in Android? -


okay, have integer variable in application. it's value of color, being set color picker in preferences. now, need use both color , darker version of color might be.

now know in standard java there color.darker() method, there doesn't seem equivalent in android. know of equivalent or workarounds?

the easiest, think, convert hsv, darkening there, , convert back:

float[] hsv = new float[3]; int color = getcolor(); color.colortohsv(color, hsv); hsv[2] *= 0.8f; // value component color = color.hsvtocolor(hsv); 

to lighten, simple approach may multiply value component > 1.0. however, you'll have clamp result range [0.0, 1.0]. also, multiplying isn't going lighten black.

therefore better solution is: reduce difference 1.0 of value component lighten:

hsv[2] = 1.0f - 0.8f * (1.0f - hsv[2]); 

this entirely parallel approach darkening, using 1 origin instead of 0. works lighten color (even black) , doesn't need clamping. simplified to:

hsv[2] = 0.2f + 0.8f * hsv[2]; 

however, because of possible rounding effects of floating point arithmetic, i'd concerned result might exceed 1.0f (by perhaps 1 bit). better stick more complicated formula.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -