OpenDocument4all - Info and tools about OpenDocument
Thursday, 11 March 2010
Home arrow OO Development & Info arrow OpenOffice programming arrow RGB color converter for OpenOffice colors
Main Menu
Home
My Blog
AODL - Project
AODC - Project
OO Development & Info
Forum
About OpenDocument ...
Search
News
Contact Us
Login Form





Lost Password?
No account yet? Register
Sourceforge.net

SourceForge.net - Goto the project homepage
Project site on SF.net

 

Demand OpenDocument

 

Countomat Webcounter und Webstatistik (Statistik & Logfileanalyse, Counter)

RGB color converter for OpenOffice colors Print E-mail
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;
   }
}.

 
< Prev   Next >
Quick jump
Download area
Who's Online
We have 4 guests online
Polls
Should AODL and AODC also implement export as PDF and RTF?
 
Popular
© 2005 MamboZ. All rights reserved.