00001
00002
00003
00004
00005
00006 static char *rcsid= "@(#)$Id: RGBHLS.c,v 1.1.1.1 2003/11/20 07:46:30 eus Exp $";
00007
00008 #include <math.h>
00009 #include "../c/eus.h"
00010
00011 #pragma init (init_object_module)
00012 pointer RGBHLS();
00013
00014 static void init_object_module()
00015 { add_module_initializer("RGBHLS", RGBHLS);}
00016
00017 void rgb_to_hls(r,g,b,h,l,s)
00018 int r,g,b;
00019 int *h, *l, *s;
00020 {
00021 int v,m,vm,r2,g2,b2;
00022 float f;
00023
00024 v = max(r,g); v=max(v,b);
00025 m = min(r,g); m=min(m,b);
00026
00027 if ((*l = (m + v) / 2) <= 0) {*h= *s=0; return;}
00028 if ((*s = vm = v-m) > 0)
00029 if (*l <=128) {
00030 f=(float)(*s)/(float)(v+m);
00031 *s = 255*f;}
00032 else {
00033 f=(float)(*s)/(float)(511-v-m);
00034 *s= 255 * f;}
00035 else {*h = 0; return;}
00036
00037 r2= 255 * (v-r) / vm;
00038 g2= 255 * (v-g) / vm;
00039 b2= 255 * (v-b) / vm;
00040
00041 if (r==v)
00042 *h = (g==m ? 5*255+b2 : 255-g2);
00043 else if (g==v)
00044 *h= (b==m ? 255+r2 : 3*255-b2);
00045 else *h = (r==m ? 3*255 + g2 : 5*255 - r2);
00046 *h /= 6;}
00047
00048
00049 void hls_to_rgb(h,l,s,r,g,b)
00050 int h,l,s;
00051 int *r, *g, *b;
00052 { float v; }
00053
00054 pointer RGB_TO_HLS(ctx,n,argv)
00055 context *ctx;
00056 int n;
00057 register pointer argv[];
00058 { int h,l,s,v;
00059 ckarg(3);
00060 rgb_to_hls(ckintval(argv[0]), ckintval(argv[1]), ckintval(argv[2]),
00061 &h, &l, &s);
00062 h=min(h,255); l=min(l,255); s=min(s,255);
00063
00064 v= (h << 16) | (l << 8) | s;
00065 return(makeint(v));}
00066
00067 pointer RGBHLS(ctx,n,argv)
00068 context *ctx;
00069 int n;
00070 pointer argv[];
00071 { pointer mod=argv[0];
00072 defun(ctx,"RGB-TO-HLS", mod, RGB_TO_HLS,NULL);
00073 return(T);
00074 }
00075