4

I have colors saved in a custom object. I need to create an Excel export (using XML) of the data. And I need to use the color for cell background colors.

To do that, I need to convert the RGB values to hex.

There are plenty of examples of doing this in Java, but those methods doesn't exist in Apex.

Any ideas how to solve this issue?

Edit:

Stored in a text field. Like so: rgb(162,34,28)

I then do something like this:

String color = 'rgb(162,34,28)';

color = color.replace('rgb(', '');
color = color.replace(')', '');

String r, g, b;
List<String> colorParts = color.split(',');
r = colorParts[0];
g = colorParts[1];
b = colorParts[2];

And then i need to get a hex value from that.

1
  • 2
    How is the RGB value stored? Can you please give an example?
    – Saroj Bera
    Commented Nov 6, 2015 at 13:08

2 Answers 2

7

Try using this class:

public class ColorConverter {

    static String[] hexMap = '0123456789ABCDEF'.split('');       

    public static String decToHex(Integer x)
    {
        if(x == 0)
            return '00';

        String result = '';

        while (x != 0)
        {
            Integer modulus = Math.mod(x, 16);

            result = hexMap[modulus] + result;          

            x /= 16;
        }

        return result.length() == 1 ? '0' + result : result;
    } 

    public static String rgbToHex(Integer r, Integer g, Integer b) {
        return '#' + decToHex(r) + decToHex(g) + decToHex(b);
    } 
}

Usage sample:

System.assertEquals('#0100FF', ColorConverter.rgbToHex(1, 0, 255));
System.assertEquals('#080517', ColorConverter.rgbToHex(8, 5, 23));
System.assertEquals('#FFFA96', ColorConverter.rgbToHex(255, 250, 150));
System.assertEquals('#000000', ColorConverter.rgbToHex(0, 0, 0));
System.assertEquals('#FFFFFF', ColorConverter.rgbToHex(255, 255, 255));
4
  • This code, while it works, is particularly verbose compared to what it could have been. In particular, it will perform up to 10 times worse than it would have if you'd just used a simple indexed array.
    – sfdcfox
    Commented Nov 6, 2015 at 17:22
  • static String[] hex = '0123456789ABCDEF'.split(''); public static String decToHex(Integer x) { String result = ''; while(x != 0) { Integer mod = Math.mod(x, 16); result += hex[mod]; x /= 16; } result = '00'+result.reverse(); return result.right(2); }
    – sfdcfox
    Commented Nov 6, 2015 at 17:29
  • @sfdcfox I agree with you. The first code sample I provided was written quickly and was poorly optimized. I've made the changes you suggested but I kept the last part of the method the same. I still believe that this version of decToHex method is more robust if you were to take it and provide a number larger than 255, (e.g. - 256, in which case your sample would return '00' instead of '100'). However, having in mind that these are colors we are talking about (0-255) I guess that wouldn't be a problem.
    – smukov
    Commented Nov 6, 2015 at 18:10
  • @smukov I'd personally offer a second parameter for the number of hex characters to clamp to, and use '0'.repeat(padCount) instead. Still, it's all about the utility of the class.
    – sfdcfox
    Commented Nov 6, 2015 at 18:15
0

You'll need to create a method to convert the values for you. Since there are solutions for this in Java, you should be able to implement it in Apex as well... That is, if you have some knowledge of Java/programming.

To convert from RGB to Hex, you'll want to get your three groups of numbers, and convert each of them into Hex, and then unite the three in a single string, preceded by a # mark.

You can find examples online, like this one.

Edit:

I haven't tested it, but Salesforce does provide a class called EncodingUtil that has a method convertToHex that receives and gives a String as input and output.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .