AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Multimedia Delphi Fast integer RGB-HSL
Thema durchsuchen
Ansicht
Themen-Optionen

Fast integer RGB-HSL

Ein Thema von WojTec · begonnen am 29. Dez 2010 · letzter Beitrag vom 30. Dez 2010
 
Medium

Registriert seit: 23. Jan 2008
3.688 Beiträge
 
Delphi 2007 Enterprise
 
#4

AW: Fast integer RGB-HSL

  Alt 30. Dez 2010, 00:49
As far as I see it, all the calculations can be done purely with integers. To keep some proper precision, this should be done at some higher scaling than 0..255, a factor of 100 or 1000 should suffice - any scale up to |scale*MaxValueAnywhere|<MaxInt may be chosen though.
Working strictly in the range 0..1 usually only makes things a whole lot easier (or merely possible in some cases), if nonlinear functions like powers or trigonometry come into play, of which none are involved here.

Going assembler does not generally yield better performance, MMX is not an option with values >255, but handmade SSE(2) might work. Downside: This should be done right where the load of calculations are performed, to avoid switching the FPU from/into SSE mode for every tiny conversion - this could even make things slower than the current solution. Thus, I'd first try to go all integer, and see how that performs.

Edit: Another thing might be de-modularizing the whole thing. There's a lot of calling to tiny helper functions going on, and iirc, Delphi doesn't inline (at least not per default, and not at all with older versions). Doing everything in one go, even if it results in much longer and possibly uglier code, may improve speed a bit, too. But certainly not as much as eliminating the floats completely, by far.

Edit2: Also, I'm a little unsure why these functions are so long. I haven't read them that thoroughly, but here's two methods I use in a C# project of mine:
Code:
public void ColorToHSV(Color c, out double h, out double s, out double v)
      {
         double r = c.R/255f;
         double g = c.G/255f;
         double b = c.B/255f;
         
         double max = Math.Max(r, Math.Max(g, b));
         double min = Math.Min(r, Math.Min(g, b));
         
         if (max.Equals(min)) h = 0; else
         if (max.Equals(r)) h = 60f*((g-b)/(max-min)); else
         if (max.Equals(g)) h = 60f*(2f+(b-r)/(max-min)); else
         if (max.Equals(b)) h = 60f*(4f+(r-g)/(max-min)); else
         h = 0;
         
         if (h<0) h += 360;
         
         if (max.Equals(0)) s = 0; else
         s = (max-min)/max;
         
         v = max;
         
         return;
      }
      
      public Color HSVToColor(double h, double s, double v)
      {
         int hi = (int)Math.Floor(h/60f);
         double f = h/60f - hi;
         double p = (v*(1-s))*255f;
         double q = (v*(1-s*f))*255f;
         double t = (v*(1-s*(1-f)))*255f;
         v *= 255f;
         Color cn = new Color();
         switch (hi) {
            case 0:
               cn = Color.FromArgb(255, (byte)v, (byte)t, (byte)p);
               break;
            case 1:
               cn = Color.FromArgb(255, (byte)q, (byte)v, (byte)p);
               break;
            case 2:
               cn = Color.FromArgb(255, (byte)p, (byte)v, (byte)t);
               break;
            case 3:
               cn = Color.FromArgb(255, (byte)p, (byte)q, (byte)v);
               break;
            case 4:
               cn = Color.FromArgb(255, (byte)t, (byte)p, (byte)v);
               break;
            case 5:
               cn = Color.FromArgb(255, (byte)v, (byte)p, (byte)q);
               break;
            case 6:
               cn = Color.FromArgb(255, (byte)v, (byte)t, (byte)p);
               break;               
         }
         return cn;
      }
It's full of floats though, and thus not meant for speed, but it seems a lot shorter and less complex than what you posted, and can most certainly even be optimized and shortened further
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)

Geändert von Medium (30. Dez 2010 um 01:04 Uhr)
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:14 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz