Index: [thread] [date] [subject] [author]
  From: Christoph Egger <Christoph_Egger@t-online.de>
  To  : ggi-develop@eskimo.com
  Date: Sat, 8 May 1999 21:55:49 +0200 (MEST)

Re: Python / GGI

On Sat, 8 May 1999, Brian S. Julin wrote:

> On Sat, 8 May 1999, Jim Meier wrote:

> The color representation is the first thing I noticed when I toyed with
> writing a Perl5 extension, too.  In OO languages, the first thing you want
> to do with different the pixel formats is make a single object to represent
> them and methods to get their representation in different color
> spaces.  Since I was only toying with it and LibGGI was very new I
> decided to put it off and wait for a color/pixelformat converting 
> helper-lib rather than implement YUV<->RGB and such in Perl5, since
> that should really be an optimized C routine.  I don't think we have 
> anything yet to do this; closest thing is CrossBlit.
> 
> --
> Brian
> 

How do you like this one?

/* hsv_to_rgb:
 *  Converts from HSV colorspace to RGB values.
 */
void hsv_to_rgb(float h, float s, float v, ggi_sint *r, ggi_sint *g, ggi_sint *b)
{
   float f, x, y, z;
   ggi_sint i;

   v *= 255.0;

   if (s == 0.0) {
      *r = *g = *b = (int)v;
   }
   else {
      while (h < 0)
         h += 360;
      h = fmod(h, 360) / 60.0;
      i = (int)h;
      f = h - i;
      x = v * (1.0 - s);
      y = v * (1.0 - (s * f));
      z = v * (1.0 - (s * (1.0 - f)));

      switch (i) {
         case 0: *r = v; *g = z; *b = x; break;
         case 1: *r = y; *g = v; *b = x; break;
         case 2: *r = x; *g = v; *b = z; break;
         case 3: *r = x; *g = y; *b = v; break;
         case 4: *r = z; *g = x; *b = v; break;
         case 5: *r = v; *g = x; *b = y; break;
      } // switch
   } // if
   } // if
} // hsv_to_rgb

/* rgb_to_hsv:
 *  Converts an RGB value into the HSV colorspace.
 */
void rgb_to_hsv(ggi_sint r, ggi_sint g, ggi_sint b, float *h, float *s, float *v)
{
   float min, max, delta, rc, gc, bc;

   rc = (float)r / 255.0;
   gc = (float)g / 255.0;
   bc = (float)b / 255.0;
   max = MAX(rc, MAX(gc, bc));
   min = MIN(rc, MIN(gc, bc));
   delta = max - min;
   *v = max;

   if (max != 0.0)
      *s = delta / max;
   else
      *s = 0.0;

   if (*s == 0.0) {
      *h = 0.0;
   }
   else {
      if (rc == max)
         *h = (gc - bc) / delta;
      else if (gc == max)
         *h = 2 + (bc - rc) / delta;
      else if (bc == max)
         *h = 4 + (rc - gc) / delta;

      *h *= 60.0;
      if (*h < 0)
         *h += 360.0;
    } // if
} // rgb_to_hsv


Christoph Egger
E-Mail: Christoph_Egger@t-online.de

Index: [thread] [date] [subject] [author]