TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Elly Henderson
from: Michael Stapleton
date: 1995-08-03 18:16:00
subject: Re: rgb to hsv

-=> On 07-27-95  20:12 you wrote to me <=-

Hi Elly,

Is that short for Elliot?

EH> Why yes I would like you to convert it to C but it'd probably be
EH> isier if you gave it to me in the for of

EH> H = R*b+G
EH> S = R+B
EH> V = B*G

Well, it's a little more complicated than that.  :) The following
function uses integer arithmetic, but does no rounding or error
checking to keep it simple.

----------------------------- Cut here -----------------------------

/*
 *  R G B 2 H S V 2
 *
 *  Convert RGB color value to Hue, Saturation & Value
 *  Integer only
 *
 *  Written by M. Stapleton of Graphic Bits
 *  Copyright 1995
 *
 *  This program may be freely distributed & modified
 *
 *  Aug  3 1995 15:04:27
 *
 */

#define TEST

#ifdef TEST
#include 
#include 
#include 
#endif

/* Some sorting macros */
#define SWAP(A, B) A^=B, B^=A, A^=B
#define SORT2(A, B) if (B 0 */
    {
        if (hsv[1] = MS * (c2 - c0) / c2)   /* Sat > 0 */
        {
            /* Find hue sector */
            if (c2==rgb[1])
                if (c0==rgb[2]) ro = 1;
                else            ro = 2;
            else if (c2==rgb[2])
                if (c0==rgb[0]) ro = 3;
                else            ro = 4;
            else
                if (c0==rgb[1]) ro = 5;
                else            ro = 0;

            /* Is color rising or falling? */
            x = ro % 2 ? c2 - c1 : c1 - c0;
            hsv[0] = MC * ro + (MC * x) / (c2 - c0);
        }
        else /* Saturation is 0, so pixel is grey
                & thus hue is irrelevant */
            hsv[0] = 0;
    }
    else /* Value is 0, so pixel is black */
        hsv[1] = hsv[0] = 0;
}

#ifdef TEST
#define MAXI 20

/* Random integer < Z */
#define RANDI(Z) (((rand() & SHRT_MAX) * (Z)) / SHRT_MAX)

int main(void)
{
    int i, j, rgb[3], hsv[3];

    for (i=0; i * Origin: The Three Amigas - better than two (3:713/615.0)
SEEN-BY: 50/99 620/243 623/630 711/401 409 410 430 510 807 808 809 932 934
SEEN-BY: 712/508 515 713/111 317 601 611 615 618 700 826 888 906 914 714/906
SEEN-BY: 800/1 7877/2809
@PATH: 713/615 888 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.