Results 1 to 17 of 17

Thread: Color convert procedures

  1. #1
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Color convert procedures

    This is my latest work and I'm quite proud of it, maybe because it took me a hell lot of a time to find out what I needed and then where it was to be found and shiat.. Next ofcourse was the differences from the languages and some dumb mistakes.. Now I'm happy to present ColorConvert procedures by Lorax - hehe..

    Code:
    procedure Color32ToRGB(Color: Integer; var Red, Green, Blue: Integer);
    begin
      Blue  := (Color and $00ff0000) shr 16;
      Green := (Color and $0000ff00) shr 8;
      Red   := (Color and $000000ff);
    end;
    
    procedure RGBToColor32(var Color: Integer; Red, Green, Blue: Integer);
    begin
      Color := Blue shl 16;
      Color := Color + Green shl 8;
      Color := Color + Red;
    end;
    
    function Max3(int1, int2, int3: Extended): Extended;
    begin
      result:= MaxE(MaxE(int1, int2), MaxE(int2, int3));
    end;
    
    function Min3(int1, int2, int3: Extended): Extended;
    begin
      result:= MinE(MinE(int1, int2), MinE(int2, int3));
    end;
    
    procedure RGBToHSL(Red, Green, Blue: Integer; var Hue, Saturation, Luminance: Extended);
    var
      tRed, tGreen, tBlue, tMax, tMin, Delta: Extended;
    begin
      tRed:= Red div 255;
      tGreen:= Green div 255;
      tBlue:= Blue div 255;
    
      tMax:= Max3(tRed, tBlue, tGreen);
      tMin:= Min3(tRed, tBlue, tGreen);
      
      Delta:= tMax - tMin;
      
      Luminance:= (tMax + tMin) div 2.0;
      
      if(tMax = tMin)then
      begin
        Saturation:= 0.0;
        Hue:= 0.0;
      end else
      begin
    
        if Luminance < 0.5 then
          Saturation:= Delta div (2 * Luminance)
        else
          Saturation:= Delta div (2 - tMax - tMin);
    
        if(tMax = tRed)then
          Hue:= 0 + (tGreen - tBlue) div Delta;
        if(tMax = tGreen)then
          Hue:= 2 + (tBlue - tRed) div Delta;
        if(tMax = tBlue)then
          Hue:= 4 + (tRed - tGreen) div Delta;
          
        Hue:= Hue div 6.0;
      end;
    
      Hue:= Hue * 100;
      Saturation:= Saturation * 100;
      Luminance:= Luminance * 100;
    end;
    
    function HueToRGB(temp1, temp2, Hue: Extended): Extended;
    begin
      if(Hue < 0)then
        Hue:= Hue + 1;
      if(Hue > 1)then
        Hue:= Hue - 1;
      if(6 * Hue < 1.0)then
        result:= temp1 + (temp2 - temp1) * 6 * Hue
      else if(2 * Hue < 1.0)then
        result:= temp2
      else if(3 * Hue < 2.0)then
        result:= temp1 + (temp2 - temp1) * ((2.0 div 3.0) - Hue) * 6
      else
        result:= temp1;
    end;
    
    procedure HSLToRGB(var Red, Green, Blue: Integer; Hue, Saturation, Luminance: Extended);
    var
      tSaturation, tLuminance, tHue, temp2, temp1: Extended;
    begin
      tSaturation:= Saturation div 100.0;
      tLuminance:= Luminance div 100.0;
      tHue:= Hue div 100.0;
      
      if(Saturation = 0)then
      begin
        Red   := Round(tLuminance * 255);
        Green := Round(tLuminance * 255);
        Blue  := Round(tLuminance * 255);
      end else
      begin
      
        if(tLuminance < 0.5)then
          temp2:= tLuminance * (1 + tSaturation)
        else
          temp2:= (tLuminance + tSaturation) - (tSaturation * tLuminance);
    
        temp1:= 2 * tLuminance - temp2;
    
        Red   := Round(255 * HueToRGB(temp1, temp2, tHue + (1.0 div 3.0)));
        Green := Round(255 * HueToRGB(temp1, temp2, tHue));
        Blue  := Round(255 * HueToRGB(temp1, temp2, tHue - (1.0 div 3.0)));
      end;
    end;

  2. #2
    Join Date
    Feb 2006
    Location
    California-Foster City
    Posts
    742
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    woooooowww..... good job lorax!
    The Welcoming Party
    Don't be a Fakawi! Get 25 hours of sleep a day!

  3. #3
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    485
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Very nice Work Lorax.

  4. #4
    Join Date
    Feb 2006
    Location
    Australia, NSW.
    Posts
    1,461
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Good job Lory D: <3

  5. #5
    Join Date
    Feb 2006
    Location
    Berkeley, CA
    Posts
    1,837
    Mentioned
    52 Post(s)
    Quoted
    60 Post(s)

    Default

    After our interesting conversation how bout you explain the inner workings to everyone else

  6. #6
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by BenLand100
    After our interesting conversation how bout you explain the inner workings to everyone else
    Okay I think that is fair, here is the whole story.

    1: I wanted to make a script that is to paint Mona Lisa, but for that I need to make all the colors from start have a predefined luminance.

    2: I wanted to convert the color format SCAR used to HSL which paint uses.

    3: So I started looking around trying to find an matching format. It took a half hour before I got an idea, so I decided to check out a procedure which I had found in the SCAR manual one month layter:
    (function ConvertColor(C: Integer): Integer; - converts Windows RGB color to Graphics32 color. Depreciated.)

    4. Graphics32 was a great break through and after some research I found their site. And a lot info about them and such.

    5. I googled around trying to find any formulare that would convert an Graphics32 color to RGB and I got a hint from the developers site itself. It wasn't very clear though because it had the Alpha value and the format would be RGBA and not RGB. Layter on through examination I found out that the alpha value wasn't needed. Herefrom Color32ToRGB.

    6. Now I needed a procedure to reverse this. And again I googled around and after 10 mins I stumbled across something that again was for RGBA, which I copied "Note I still used Color32ToRGBA at that time". It was when I had RGBAToColor32 and Color32ToRGBA that my research got bonus, after some minuts of searching for an convert procedure from RGBToColor32 (no result) I made my own research, founding out that the alpha value had no influence, therefore I could easily change format from RGBA to RGB, which was my wanted format for now.

    7. But I needed the format HSL and not RGB, and again that lead me to a lot of research. Again Google was my friend I found out about easyRGB.com through a lot of different sites, and they themselves had the formula for converting RGB colors to HSL and HSL to RGB.

    8. I just had one problem, I combined two different procedures I had RGBToHSL from one site and HSLToRGB from another, of course they were in different languages so I had a lot of modifying to do. The main problem was that the max value of Hue, Saturation and Luminance is not the same at all places, sometimes they're 1 sometimes 360(degrees) and sometimes 6, like in my case. So I had to actually make the Hue format different.. Note that the max value of Hue at the place from where I got RGBToHSL, were hard to find..

    8. So once I had got the right format I had another problem

    9. the formula from easyRGB.com was in VB, and in VB once you divide 1 with 3 you get 0.33333 but in SCAR you get 0. It took my mate Liquid 2 mins to figure this out and it was only once I asked him about it I figured it out myself. in SCAR 1.0 div 3.0 gives 0.3333333 that is because the format is float and not an integer like in 1 div 3 and the calculation way is different.

    But AFTER like 4-5 hours of research and mind trouble I got it right..

    Edit: oh and Kane "lory" is a terrible nick name hehe

  7. #7
    Join Date
    Feb 2006
    Location
    Under a rock.
    Posts
    1,351
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Wow, very interesting, I would have never been able to think up somethign like this, especially attempting at doing it!!

    Awesome job Lorax!
    SRL Developer
    ◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

  8. #8
    Join Date
    Feb 2006
    Location
    Berkeley, CA
    Posts
    1,837
    Mentioned
    52 Post(s)
    Quoted
    60 Post(s)

    Default

    It just goes to show you can do anything with google

  9. #9
    Join Date
    Feb 2006
    Location
    California-Foster City
    Posts
    742
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    come on ben be nice. loraz is just teasing you
    The Welcoming Party
    Don't be a Fakawi! Get 25 hours of sleep a day!

  10. #10
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by BenLand100
    It just goes to show you can do anything with google
    If I may correct you, this shows that anything can be done with an awesome brain and google

  11. #11
    Join Date
    Feb 2006
    Location
    Berkeley, CA
    Posts
    1,837
    Mentioned
    52 Post(s)
    Quoted
    60 Post(s)

    Default

    Quote Originally Posted by Lorax
    If I may correct you, this shows that anything can be done with an awesome brain and google
    You just seemed to have missed the part of actualy doing the conversion in your explination

  12. #12
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by BenLand100
    You just seemed to have missed the part of actualy doing the conversion in your explination
    what are you talking about ?!

  13. #13
    Join Date
    Feb 2006
    Location
    California-Foster City
    Posts
    742
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    people. please stop arguing/teasing/whatever, its counterproductive.
    The Welcoming Party
    Don't be a Fakawi! Get 25 hours of sleep a day!

  14. #14
    Join Date
    Feb 2006
    Posts
    433
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Wow, very nice !

  15. #15
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Sdcit
    people. please stop arguing/teasing/whatever, its counterproductive.
    Don't worry me and benland are just kidding around.. We've had our differences but they're gone. I got nothing on benland..

  16. #16
    Join Date
    Feb 2006
    Posts
    51
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    And Liquid.

  17. #17
    Join Date
    Feb 2006
    Posts
    920
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Liquid
    And Liquid.
    Just wait Liquid, I will dig something up * catches phone and dials special agent*

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. convert caps
    By Waddo in forum Research & Development Lounge
    Replies: 10
    Last Post: 08-28-2008, 10:51 AM
  2. How do I convert A String into a Varible?
    By gamer 5 in forum OSR Help
    Replies: 3
    Last Post: 02-08-2008, 03:01 AM
  3. what do i DO (convert 3.11 to 3.12 script)
    By P1nky in forum OSR Help
    Replies: 2
    Last Post: 09-28-2007, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •