?
Solved

Colour Model Conversions

Posted on 1997-04-21
2
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

777 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