Delphi color conversion routines in GraphUtil.pas

I read Zarko Gajic’s and he frequently publishes interesting articles related to Delphi. He does a great job supplying the Delphi community with a constant stream of useful programming tips. His latest post Convert Delphi’s TColor to HTML Color Value has a great tip about converting color values. After reading this post it dawned on me that I’ve never mentioned the routines we’ve added to the Delphi RTL for converting colors. We also added TColor values for all of the named web colors.

You can find all of these routines in GraphUtil.pas.

{ Converts a TColor to a Web color constant like #FFFFFF }
function ColorToWebColorStr(Color: TColor): string;

{ Converts a TColor to a Web color name, returns a Web color value if the color is not a match. }
function ColorToWebColorName(Color: TColor): string;
function WebColorToRGB(WebColor: Integer): Integer;
function RGBToWebColorStr(RGB: Integer): string;
function RGBToWebColorName(RGB: Integer): string;

{ Converts a Web color name to its TColor equivalent, returns clNone if no match }
function WebColorNameToColor(WebColorName: string): TColor;

{ Converts a web style color string (#FFFFFF or FFFFFF) to a TColor }
function WebColorStrToColor(WebColor: string): TColor;

8 thoughts on “Delphi color conversion routines in GraphUtil.pas

  1. > What about RGB to HLS and back conversion? 🙂
    I use the JclGraphics unit for this.
    Merry Christmas.

  2. Hey Steve,
    I wonder if we’ve ever mentioned what the original name for GraphUtil.pas was? It went for quite a while as GraphicsEx.pas… until someone pointed out that swapping the casing for the ‘s’ and the ‘E’ made that unit name completely inappropriate and rude. We could not stop laughing about it for hours… Even now we have a little chuckle to ourselves everytime we see that name and know from where it came ;-).

  3. Hey Allen,
    Yes, too funny! I definitely thought about it and laughed once again just posting this entry. Having come up with the original name I’ll never forget when the obvious was pointed out to me. Still, I think we might have gotten away with it for awhile. 🙂

  4. > We also added TColor values for all of the named web colors.
    All 16 of them? Wow!
    Such generosity is sure to be rewarded.

Comments are closed.