|
|
|
RGB color converter for OpenOffice colors |
|
|
|
Written by Lars Behrmann
|
|
Thursday, 29 December 2005 |
If you develop applications for OpenOffice and get a color property value e.g. CellBackground then you will recieve the color as an RGB color. Now, you want to have this color converted to a Hex color, but OpenOffice doesn't offer you a mehod to do that. In this case you can use my RGBConverter class. It is available as a .net / C# and Java solution.
I think the usage is really simple so that it need no further statements. Just copy the source and use it. Java version: import java.lang.*; public class RGBConverter { /* * Use me like this RGBConverter.ToHexColor(myRgbColor) */ public static String ToHexColor(String rgbColor) { int length = rgbColor.length(); if(length < 9) { int fillCnt = 9-length; StringBuilder fillStr = new StringBuilder(rgbColor); for(int i=0; i<fillCnt; i++) fillStr = fillStr.insert(0, "0"); rgbColor = fillStr.toString(); } String rColor = rgbColor.substring(0, 3); String gColor = rgbColor.substring(3, 6); String bColor = rgbColor.substring(6); Integer irColor = new Integer(rColor); Integer igColor = new Integer(gColor); Integer ibColor = new Integer(bColor); rColor = Integer.toHexString(irColor.intValue()); gColor = Integer.toHexString(igColor.intValue()); bColor = Integer.toHexString(ibColor.intValue()); rgbColor = "#" +((rColor.equals("0"))?"00":rColor) +((gColor.equals("0"))?"00":gColor) +((bColor.equals("0"))?"00":bColor); return rgbColor; } } C# version: public class RGBConverter { //Use me like this RGBConverter.ToHexColor(anRGBColor) public static string ToHexColor(string rgbColor) { //need leading zeros ? if(rgbColor.Length < 9) //fill up with leading zeros rgbColor = rgbColor.PadLeft(9, '0'); //our rgb values int rValue = Convert.ToInt32(rgbColor.Substring(0,3)); int gValue = Convert.ToInt32(rgbColor.Substring(3,3)); int bValue = Convert.ToInt32(rgbColor.Substring(6)); //convert into hex string srValue = ((rValue!=0)?rValue.ToString("x"):"00"); string sgValue = ((gValue!=0)?gValue.ToString("x"):"00"); string sbValue = ((bValue!=0)?bValue.ToString("x"):"00"); //build hex color string hexColor = "#" //fill with leading zeros, if needed + srValue.PadLeft(2, '0') + sgValue.PadLeft(2, '0') + sbValue.PadLeft(2, '0'); return hexColor; } }. |
|
|
Who's Online |
|
We have 2 guests online |
|