home *** CD-ROM | disk | FTP | other *** search
- #include <intuition/intuition.h>
- #include <libraries/gadtools.h>
- #include <clib/macros.h>
- #include <stdlib.h>
- #include <clib/gadtools_protos.h>
- #include <clib/graphics_protos.h>
-
-
- #include <stdio.h>
-
- #include "protos.h" /* Routine prototypes */
- #include "gadgets.h" /* GadToolsBox file */
- #include "defs.h"
-
- extern struct Screen *Scr; /* screen we open up on */
- extern UBYTE CSIsOpen; /* window open? */
- extern USHORT ColorMode; /* Color selection mode */
- extern UWORD RedVal; /* RGB red value */
- extern UWORD GreenVal; /* RGB green value */
- extern UWORD BlueVal; /* RGB blue value */
- extern UWORD HueLevel; /* HSV hue */
- extern UWORD SatLevel; /* HSV saturation */
- extern UWORD ValLevel; /* HSV value */
- extern SHORT CurrentColor; /* Currently selected color */
- extern ULONG NumColors; /* # of colors in palette */
-
- /*=============================================================================
- * Set the slider props to reflect the proper position for the supplied color
- * number. If the window is not open (I.E. called from ARexx) then disregard
- *===========================================================================*/
-
- void SetProps(SHORT num)
- {
-
- if(!CSIsOpen) return; /* If called from ARexx and window hid */
-
- GetColors(num);
- if(ColorMode == RGB_COLORMODE)
- {
- GT_SetGadgetAttrs(GAD(GD_RED_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)RedVal,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Level,(WORD)GreenVal,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_BLUE_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)BlueVal,TAG_END);
- }
- else
- {
- CalcHSV(RedVal,GreenVal,BlueVal);
- GT_SetGadgetAttrs(GAD(GD_RED_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)HueLevel,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Level,(WORD)SatLevel,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_BLUE_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)ValLevel,TAG_END);
- }
- }
-
-
- /*=============================================================================
- * Set the prop size and identifiers to either RGB or HSV
- *===========================================================================*/
-
- void SetColorMode(USHORT mode)
- {
-
- if(!CSIsOpen) return; /* If called from ARexx and window is hid */
-
- if(mode == RGB_COLORMODE)
- {
-
- GT_SetGadgetAttrs(GAD(GD_RGBHSV_GAD),ColorSaverWnd,NULL,GTMX_Active,(UWORD)0,TAG_END);
-
- GT_SetGadgetAttrs(GAD(GD_RGBR_GAD),ColorSaverWnd,NULL,GTTX_Text,"R",TAG_END);
- GT_SetGadgetAttrs(GAD(GD_RGBG_GAD),ColorSaverWnd,NULL,GTTX_Text,"G",TAG_END);
- GT_SetGadgetAttrs(GAD(GD_RGBB_GAD),ColorSaverWnd,NULL,GTTX_Text,"B",TAG_END);
-
- GT_SetGadgetAttrs(GAD(GD_RED_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_BLUE_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
-
- }
- else
- {
-
- GT_SetGadgetAttrs(GAD(GD_RGBHSV_GAD),ColorSaverWnd,NULL,GTMX_Active,(UWORD)1,TAG_END);
-
- GT_SetGadgetAttrs(GAD(GD_RGBR_GAD),ColorSaverWnd,NULL,GTTX_Text,"H",TAG_END);
- GT_SetGadgetAttrs(GAD(GD_RGBG_GAD),ColorSaverWnd,NULL,GTTX_Text,"S",TAG_END);
- GT_SetGadgetAttrs(GAD(GD_RGBB_GAD),ColorSaverWnd,NULL,GTTX_Text,"V",TAG_END);
-
- GT_SetGadgetAttrs(GAD(GD_RED_GAD),ColorSaverWnd,NULL,GTSL_Max,359,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Max,100,TAG_END);
- GT_SetGadgetAttrs(GAD(GD_BLUE_GAD),ColorSaverWnd,NULL,GTSL_Max,100,TAG_END);
- }
- }
-
- /*=============================================================================
- * Get the current color bits for the specified color number
- *===========================================================================*/
-
- void GetColors(SHORT num)
- {
- UWORD col;
-
- col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)num);
- RedVal = (col >> 8) & 0x0F;
- GreenVal= (col >> 4) & 0x0F;
- BlueVal = (col ) & 0x0F;
-
- }
-
- /*=============================================================================
- * Set the current palette color to the specified color number
- *===========================================================================*/
-
- void SetPaletteColor(SHORT color)
- {
-
- CurrentColor = COLORValid(color,NumColors,CurrentColor);
-
- if(!CSIsOpen) return; /* If called from ARexx and window is hid */
-
- GT_SetGadgetAttrs(GAD(GD_PALETTE_GAD), ColorSaverWnd,NULL,GTPA_Color,
- color,TAG_END);
- }
-
- /*=============================================================================
- * Shift the entire color palette one color to the left. I.E, for a 4-color
- * palette, COLOR 0 becomes COLOR 3, COLOR 2 becomes COLOR 1, etc...
- *===========================================================================*/
-
- void ShiftColorsLeft( void )
- {
- register SHORT i,j;
-
- UWORD firstred;
- UWORD firstgreen;
- UWORD firstblue;
-
- GetColors(0);
-
- firstred = RedVal;
- firstgreen = GreenVal;
- firstblue = BlueVal;
-
- for (i = 0, j=1; i < NumColors; i++,j++)
- {
- if(i == NumColors-1) /* last color to be set */
- {
- RedVal = firstred;
- GreenVal = firstgreen;
- BlueVal = firstblue;
- }
- else
- GetColors(j);
-
- SetRGB4(&Scr->ViewPort,i,RedVal,GreenVal,BlueVal);
-
- }
- }
-
- /*=============================================================================
- * Shift the entire color palette one color to the right. I.E, for a 4-color
- * palette, COLOR 0 becomes COLOR 1, COLOR 3 becomes COLOR 0, etc...
- *===========================================================================*/
- void ShiftColorsRight( void )
- {
- register SHORT i,j;
-
- UWORD firstred;
- UWORD firstgreen;
- UWORD firstblue;
-
- GetColors(NumColors-1);
-
- firstred = RedVal;
- firstgreen = GreenVal;
- firstblue = BlueVal;
-
- for (i = NumColors-1, j=NumColors-2; i > -1; --i,j--)
- {
- if(i == 0) /* last color to be set */
- {
- RedVal = firstred;
- GreenVal = firstgreen;
- BlueVal = firstblue;
- }
- else
- GetColors(j);
-
- SetRGB4(&Scr->ViewPort,i,RedVal,GreenVal,BlueVal);
-
- }
- }
-
- /*=============================================================================
- * Spread the colors over a specified range
- *===========================================================================*/
-
- void ColorRange( SHORT first, SHORT last )
- {
- register SHORT i;
- LONG whole, redfraction, greenfraction, bluefraction;
- UWORD col;
- UWORD firstred, firstgreen, firstblue;
- UWORD lastred, lastgreen, lastblue;
- UWORD workred, workgreen, workblue;
-
- if (first > last) {
- i = first;
- first = last;
- last = i;
- }
-
- /* I need to see a spread of at least two, where there's at least one
- * spot between the endpoints, else there's no work to do so I
- * might as well just return now.
- */
-
- if (first >= last - 1)
- return;
-
- col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)first);
-
- firstred = (col >> 8) & 0x0F;
- firstgreen = (col >> 4) & 0x0F;
- firstblue = (col ) & 0x0F;
-
- col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)last);
-
- lastred = (col >> 8) & 0x0F;
- lastgreen = (col >> 4) & 0x0F;
- lastblue = (col ) & 0x0F;
-
- whole = ((LONG) (lastred - firstred)) << 16;
- redfraction = whole / (last - first);
- whole = ((LONG)(lastgreen - firstgreen)) << 16;
- greenfraction = whole / (last - first);
- whole = ((LONG)(lastblue - firstblue)) << 16;
- bluefraction = whole / (last - first);
-
- for (i = first + 1; i < last; i++)
- {
- lastred = (redfraction * (i - first) + 0x8000) >> 16;
- workred = firstred + lastred;
- lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
- workgreen = firstgreen + lastgreen;
- lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
- workblue = firstblue + lastblue;
- SetRGB4(&Scr->ViewPort,i,workred,workgreen,workblue);
- }
- }
-
-
- #define MAX3(a,b,c) MAX(a,MAX(b,c))
- #define MIN3(a,b,c) MIN(a,MIN(b,c))
-
- /*=============================================================================
- * The following two routines are slightly modified versions of routines
- * heisted from "HSV" by Frank Ederveen (FF791). Thanks for the good
- * explanation of how HSV works, Frank. I really hadn't much of an idea,
- * and never really thought of adding HSV to ColorSaver until I saw your
- * program....
- *===========================================================================*/
-
- void CalcHSV ( UWORD red, UWORD green, UWORD blue )
- {
- float hue, sat, delta, r, g, b, max, min;
-
- r = ((float) red ) / 15;
- g = ((float) green) / 15;
- b = ((float) blue ) / 15;
-
- max = MAX3(r,g,b);
- min = MIN3(r,g,b);
-
- ValLevel = 100 * max;
-
- if (max != 0)
- sat = (max - min) / max;
- else
- sat = 0;
-
- if (sat == 0)
- hue = 0;
- else
- {
- delta = max - min;
-
- if (r == max)
- hue = (g - b) / delta;
- else if (g == max)
- hue = 2 + (b - r) / delta;
- else
- hue = 4 + (r - g) / delta;
-
- hue *= 60;
-
- if (hue < 0)
- hue += 360;
- }
- HueLevel = hue;
- SatLevel = 100 * sat;
-
- }
-
- void CalcRGB( void )
- {
- float hue, sat, val, r, g, b, p1, p2, p3;
- int i = 0;
- float f = 0;
-
- hue = (float) HueLevel / 60;
- val = (float) ValLevel / 100;
- sat = (float) SatLevel / 100;
-
- while ((f = hue - i) > 1)
- i++;
-
- p1 = val * (1 - sat);
- p2 = val * (1 - (sat * f));
- p3 = val * (1 - (sat * (1 - f)));
-
- switch (i)
- {
- case 0:
- r = val;
- g = p3;
- b = p1;
- break;
- case 1:
- r = p2;
- g = val;
- b = p1;
- break;
- case 2:
- r = p1;
- g = val;
- b = p3;
- break;
- case 3:
- r = p1;
- g = p2;
- b = val;
- break;
- case 4:
- r = p3;
- g = p1;
- b = val;
- break;
- case 5:
- r = val;
- g = p1;
- b = p2;
- break;
- }
-
- RedVal = (UWORD) (r * 15);
- GreenVal = (UWORD) (g * 15);
- BlueVal = (UWORD) (b * 15);
-
- }
-