How to convert 3-digit HTML hex colors to 6-digit flex hex colors

18,704

Solution 1

The three digit hex colors are expanded by doubling each digit (see w3 spec). So #F3A gets expanded to #FF33AA.

Solution 2

Double every digit: for example #A21 is equal to #AA2211.

However this question is a duplicate of: convert to 3-digit hex color code

Solution 3

Other answers provided the process but I will provide the code using regex and the java programming language

String value = "#FFF";
value = value.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");

Solution 4

If you came here and is using Python, here's how:

hex_code = '#FFF'
new_hex_code = '#{}'.format(''.join(2 * c for c in hex_code.lstrip('#')))

Solution 5

Kotlin version of Ovokerie's answer:

shortHexString.replace(Regex("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])"), "#$1$1$2$2$3$3")
Share:
18,704
Trinu
Author by

Trinu

Updated on June 07, 2022

Comments

  • Trinu
    Trinu about 2 years

    I want to convert a three-digit hex color which is coming from HTML CSS to a six-digit hex color for Flex. Can anyone give me code to convert 3-digit hex colors to their 6-digit equivalents?