UIColor to [Android] Hex Converter

Recently I worked on a project porting an iOS app to Android.

In iOS you create colors using the UIColor class, with calls like:

[UIColor colorWithRed:0.3 green:0.85 blue:0.41 alpha:0.75];

In Android you must specify colors via hex. So the task is to convert iOS UIColor calls to hex.


How It Works

It's straight-forward once you wrap your head around it.

Each color float represents a number between 0 and 255, so in
[UIColor colorWithRed:0.3 green:0.85 blue:0.41 alpha:0.75];

red = 0.3 * 255 = 76.5, rounded to 77

Once all numbers are normalized to 0-255 than you just convert each to base-16.

Walking through with 77 from above:

77 in base-16 = 4D

Recall that in Android the format is ARGB

.