# android - Hex transparency in colors

ID : 847

viewed : 174

### Top 5 Answer for android - Hex transparency in colors  100

Here's a correct table of percentages to hex values. E.g. for 50% white you'd use #80FFFFFF.

• 100% — FF
• 95% — F2
• 90% — E6
• 85% — D9
• 80% — CC
• 75% — BF
• 70% — B3
• 65% — A6
• 60% — 99
• 55% — 8C
• 50% — 80
• 45% — 73
• 40% — 66
• 35% — 59
• 30% — 4D
• 25% — 40
• 20% — 33
• 15% — 26
• 10% — 1A
• 5% — 0D
• 0% — 00

(source)  80

You can see the full table of percentages to hex values and run the code in this playground in https://play.golang.org/p/l1JaPYFzDkI .

# Short explanation in pseudocode

### Percentage to hex values

1. decimal = percentage * 255 / 100 . ex : decimal = 50*255/100 = 127.5
2. convert decimal to hexadecimal value . ex: 127.5 in decimal = 7*16ˆ1 + 15 = 7F in hexadecimal

### Hex values to percentage

1. convert the hexaxdecimal value to decimal. ex: D6 = 13*16ˆ1 + 6 = 214
2. percentage = (value in decimal ) * 100 / 255. ex : 214 *100/255 = 84%

The problem can be solved generically by a cross multiplication.

We have a percentage (ranging from 0 to 100 ) and another number (ranging from 0 to 255) then converted to hexadecimal.

• 100 <==> 255 (FF in hexadecimal)
• 0 <==> 0 (00 in hexadecimal)

For 1%

• 1 * 255 / 100 = 2,5
• 2,5 in hexa is 2 if you round it down.

For 2%

• 2 * 255 / 100 = 5
• 5 in hexa is 5 .

The table in the best answer gives the percentage by step of 5%.

How to calculate the numbers between in your head ? Due to the 2.5 increment, add 2 to the first and 3 to the next

• 95% — F2 // start
• 96% — F4 // add 2 to F2
• 97% — F7 // add 3 . Or F2 + 5 = F7
• 98% — F9 // add 2
• 99% — FC // add 3. 9 + 3 = 12 in hexa : C
• 100% — FF // add 2

I prefer to teach how to find the solution rather than showing an answer table you don't know where the results come from.

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime  73

Color hexadecimal notation is like following: #AARRGGBB

• A : alpha
• R : red
• G : green
• B : blue

You should first look at how hexadecimal works. You can write at most FF.  69

That chart is not showing percents. "#90" is not "90%". That chart shows the hexadecimal to decimal conversion. The hex number 90 (typically represented as 0x90) is equivalent to the decimal number 144.

Hexadecimal numbers are base-16, so each digit is a value between 0 and F. The maximum value for a two byte hex value (such as the transparency of a color) is 0xFF, or 255 in decimal. Thus 100% is 0xFF.  55

I built this small helper method for an android app, may come of use:

`` /**  * @param originalColor color, without alpha  * @param alpha         from 0.0 to 1.0  * @return  */ public static String addAlpha(String originalColor, double alpha) {     long alphaFixed = Math.round(alpha * 255);     String alphaHex = Long.toHexString(alphaFixed);     if (alphaHex.length() == 1) {         alphaHex = "0" + alphaHex;     }     originalColor = originalColor.replace("#", "#" + alphaHex);       return originalColor; } ``