Solved

Colour Model Conversions

Posted on 1997-04-21
2
187 Views
Last Modified: 2010-04-04
Does anybody out there know a decent RGB to HSL color model conversion algorithm.
0
Comment
Question by:StevenB
2 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1335564
This is NOT Delphi code as you can see.
There should not be a problem for you to port
this though.
From "Fundamentals of Interactive Computer Graphics" by Foley and van Dam:


void RGBtoHSL( COLORREF rgb, double *H, double *S, double *L )
{
   double delta;
   double r = (double)GetRValue(rgb)/255;
   double g = (double)GetGValue(rgb)/255;
   double b = (double)GetBValue(rgb)/255;
   double cmax = max(r,max(g,b));
   double cmin = min(r,min(g,b));
   *L=(cmax+cmin)/2.0;
   if(cmax==cmin) {
      *S = 0;
      *H = 0; // it's really undefined
   } else {
      if(*L < 0.5)
         *S = (cmax-cmin)/(cmax+cmin);
      else
         *S = (cmax-cmin)/(2.0-cmax-cmin);
      delta = cmax - cmin;
      if(r==cmax)
          *H = (g-b)/delta;
      else if(g==cmax)
          *H = 2.0 +(b-r)/delta;
      else
          *G=4.0+(r-g)/delta;
      *H /= 6.0;
      if(*H < 0.0)
          *H += 1;
   }
}

To convert HSL to RGB use code like this:

static double HuetoRGB(double m1, double m2, double h )
{
   if( h < 0 ) h += 1.0;
   if( h > 1 ) h -= 1.0;
   if( 6.0*h < 1 )
      return (m1+(m2-m1)*h*6.0);
   if( 2.0*h < 1 )
      return m2;
   if( 3.0*h < 2.0 )
      return (m1+(m2-m1)*((2.0/3.0)-h)*6.0);
   return m1;
}

COLORREF HLStoRGB( double H, double L, double S )
{
  double r,g,b;
  double m1, m2;

  if(S==0) {
     r=g=b=L;
  } else {
     if(L <=0.5)
        m2 = L*(1.0+S);
     else
        m2 = L+S-L*S;
     m1 = 2.0*L-m2;
     r = HuetoRGB(m1,m2,H+1.0/3.0);
     g = HuetoRGB(m1,m2,H);
     b = HuetoRGB(m1,m2,H-1.0/3.0);
  }
  return RGB((BYTE)(r*255),(BYTE)(g*255),(BYTE)(b*255));
}


0
 
LVL 4

Author Comment

by:StevenB
ID: 1335565
Cheers, Steven.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 90
LAN or WAN ? 11 92
How to make Sign in, using Clientdataset? 1 19
Firemonkey BASS_Init into a thread 17 9
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question