[comp.sources.atari.st] v01i056: color -- allow user to edit color palette

koreth@ssyx.ucsc.edu (Steven Grimm) (06/15/88)

Submitted-by: uunet!van-bc!resrch!bobs (Robert J. Silliker)
Posting-number: Volume 1, Issue 56
Archive-name: color

[This is the source to a dialog box handler that lets the user
 modify the color palette.  Resource file is included and will
 be uudecoded automatically by the shar file. -sg]

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  COLOR.C
#	  COLOR.DEF
#	  COLOR.DOC
#	  COLOR.H
#	  COLOR.RSC
#	  MAKEFILE
#	  README
#	  TEST.C
#
sed 's/^X//' << 'SHAR_EOF' > COLOR.C &&
X/***************************************************/
X/*  						   */
X/*  			COLOR.C  		   */
X/*  						   */
X/*  Copyright (c) 1988, by Robert Joseph Silliker  */
X/*  		  All Rights Reserved   	   */
X/*  	Permission is granted for unrestricted     */
X/*  		 non-commercial use		   */
X/*  						   */
X/***************************************************/
X
X#include <osbind.h>
X#include <gemdefs.h>
X#include <obdefs.h>
X#include "color.h"
X
X#include "objects.c"
X
X
Xint work_rgb[16][3];	/* Current settings for the colors */
Xint old_rgb[16][3];	/* Color settings at initialization */
Xint color_set = 0;	
Xint color_arrow[] = {ARROW0, ARROW1, ARROW2, ARROW3, ARROW4, ARROW5,
X		     ARROW6, ARROW7, ARROW8, ARROW9, ARROW10, ARROW11,
X		     ARROW12, ARROW13, ARROW14, ARROW15};
X
Xint color_buttons[3][8] = {{COLOR1_8, COLOR1_7, COLOR1_6, COLOR1_5,
X			    COLOR1_4, COLOR1_3, COLOR1_2, COLOR1_1},
X			   {COLOR2_8, COLOR2_7, COLOR2_6, COLOR2_5,
X			    COLOR2_4, COLOR2_3, COLOR2_2, COLOR2_1},
X			   {COLOR3_8, COLOR3_7, COLOR3_6, COLOR3_5,
X			    COLOR3_4, COLOR3_3, COLOR3_2, COLOR3_1}};
X
Xint color_levels[] = {0, 213, 356, 499, 642, 785, 928, 1000};
Xint mono_levels[] = {0, 1000};
X
X#define HIRES 2
X#define MEDRES 1
X#define LORES 0
X
Xstatic int set_columns();
Xstatic int set_col();
Xstatic int copy_color();
X
Xstatic int max_colors;
Xstatic int init_done = 0;
Xstatic int mode;
Xstatic OBJECT *color_object;
X
X/*------------------------------------*/
X/* Display the color dialog and allow */
X/* the user to interact with it.      */
X/*------------------------------------*/
X
Xdo_color(handle)
Xint handle;
X{
X  int save_rgb[16][3];
X  register int i, j, ob;
X  int oldob;
X
X  if(!init_done) {
X    mode = Getrez();
X
X    if(mode == LORES)
X      max_colors = 16;
X    else if(mode == MEDRES)
X      max_colors = 4;
X    else
X      max_colors = 1;
X
X    /*------------------------------------------------------*/
X    /* Convert the objects from char coords to pixel coords */
X    /*------------------------------------------------------*/
X
X    b_fix_objects();
X
X    color_object = b_rs_trindex[TSETCOLR];
X  }
X
X  copy_color(save_rgb, work_rgb);
X  set_columns(color_set, 0);
X
X  chg_obj(color_object, color_arrow[0], NORMAL, 0);
X
X  for(i=1; i<4; i++) {
X    if(mode == HIRES)
X      chg_obj(color_object, color_arrow[i], DISABLED, 0);
X    else
X      chg_obj(color_object, color_arrow[i], NORMAL, 0);
X  }
X
X  for(i=4; i<16; i++) {
X    if(mode != LORES)
X      chg_obj(color_object, color_arrow[i], DISABLED, 0);
X    else
X      chg_obj(color_object, color_arrow[i], NORMAL, 0);
X  }
X
X  oldob = color_arrow[color_set];
X  chg_obj(color_object, oldob, OUTLINED, 0);
X
X  if(!init_done) {
X    form_center(color_object, &color_object->ob_x, &color_object->ob_y,
X                            &color_object->ob_width, &color_object->ob_height);
X  }
X
X  form_dial(FMD_START, 0, 0, 0, 0, color_object->ob_x-3,
X				   color_object->ob_y-3,
X				   color_object->ob_width+6,
X				   color_object->ob_height+6);
X
X  objc_draw(color_object, 0, MAX_DEPTH, color_object->ob_x-3,
X					color_object->ob_y-3,
X					color_object->ob_width+6,
X					color_object->ob_height+6);
X  
X  do {
X    ob = form_do(color_object, 0);
X    if(ob == COLORCAN) {
X      for(i=0; i<3; i++)
X        set_col(i, color_set, NORMAL, 0);
X      copy_color(work_rgb, save_rgb);
X
X      for(i=0; i<max_colors; i++)
X        vs_color(handle, i, &work_rgb[i][0]);
X    }
X    else if(ob != COLOROK) {
X      for(i=0; i<max_colors; i++) {
X        if(color_arrow[i] == ob) {
X          set_columns(i, 1);
X          break;
X        }
X      }
X      if(i < max_colors) {
X        (color_object+oldob)->ob_state = NORMAL;
X        (color_object+ob)->ob_state = OUTLINED;
X
X	draw_button(ob);
X	draw_button(oldob);
X
X        oldob = ob;
X      }
X      else {
X        for(i=0; i<3; i++) {
X          for(j=0; j<8; j++) {
X            if(color_object[color_buttons[i][j]].ob_state & SELECTED) {
X              work_rgb[color_set][i] = color_levels[j];
X              break;
X            }
X          }
X        }
X        vs_color(handle, color_set, &work_rgb[color_set][0]);
X      }
X    }
X  } while((ob != COLOROK) && (ob != COLORCAN));
X  
X  chg_obj(color_object, ob, NORMAL, 0);
X  
X  form_dial(FMD_FINISH, 0, 0, 0, 0, color_object->ob_x-3,
X				    color_object->ob_y-3,
X				    color_object->ob_width+6,
X				    color_object->ob_height+6);
X
X  init_done = 1;
X}
X
X/*-----------------------------*/
X/* Save or restore the colors. */
X/* Flag  = 0 - Restore.        */
X/* Flag != 0 - Save.           */
X/*-----------------------------*/
X
Xsave_color(handle, flag)
Xint handle, flag;
X{
X  register int i;
X
X  for(i=0; i<16; i++) {
X    if(flag) {
X      vq_color(handle, i, 0, &work_rgb[i][0]);
X      vq_color(handle, i, 0, &old_rgb[i][0]);
X    }
X    else
X      vs_color(handle, i, &old_rgb[i][0]);
X  }
X}
X
Xstatic int set_columns(color, flag)
Xint color, flag;
X{
X  register int i;
X
X  for(i=0; i<3; i++)
X    set_col(i, color_set, NORMAL, flag);
X
X  for(i=0; i<3; i++)
X    set_col(i, color, SELECTED, flag);
X
X  color_set = color;
X}
X
Xstatic int set_col(index, color, state, flag)
Xint index, color;
Xint state, flag;
X{
X  register int color_index;
X
X  color_index = (work_rgb[color][index] + 72) / 143;
X
X  if(color_index < 0)
X    color_index = 0;
X  else if(color_index > 7)
X    color_index = 7;
X
X  chg_obj(color_object, color_buttons[index][color_index], state, flag);
X}
X
Xstatic int copy_color(d, s)
Xint d[][3], s[][3];
X{
X  register int i, j;
X
X  for(i=0; i<16; i++) {
X    for(j=0; j<3; j++)
X      d[i][j] = s[i][j];
X  }
X}
X
X/*
Xcenter_form(p)
XOBJECT *p;
X{
X  form_center(p, &p->ob_x, &p->ob_y, &p->ob_width, &p->ob_height);
X}
X
Xdraw_obj(p, ob)
Xregister OBJECT *p;
Xint ob;
X{
X  objc_draw(p, ob, MAX_DEPTH, p->ob_x, p->ob_y,
X                                       p->ob_width, p->ob_height);
X}
X
Xdial_obj(flag, p)
Xint flag;
XOBJECT *p;
X{
X  form_dial(flag, 0, 0, 0, 0, p->ob_x, p->ob_y,
X                                       p->ob_width, p->ob_height);
X}
X*/
X
Xchg_obj(p, ob, state, flag)
XOBJECT *p;
Xint ob, state, flag;
X{
X  objc_change(p, ob, 0, p->ob_x, p->ob_y,
X                                 p->ob_width, p->ob_height, state, flag);
X}
X
Xdraw_button(ob)
Xint ob;
X{
X  int x, y;
X
X  objc_offset(color_object, ob, &x, &y);
X
X  objc_draw(color_object, ARROWBOX, MAX_DEPTH, x-3, y-3,
X            (color_object+ob)->ob_width+6, (color_object+ob)->ob_height+6);
X}
X
X/* END OF FILE */
SHAR_EOF
chmod 0600 COLOR.C || echo "restore of COLOR.C fails"
sed 's/^X//' << 'SHAR_EOF' > COLOR.DEF &&
XSHAR_EOF
chmod 0600 COLOR.DEF || echo "restore of COLOR.DEF fails"
sed 's/^X//' << 'SHAR_EOF' > COLOR.DOC &&
XCOLOR (3L)							COLOR(3L)
X
X
XNAME
X	COLOR - Library routines to manipulate the color palette from
X		within a 'C' program.
X
XAUTHOR
X	Bob Silliker
X
XCOPYRIGHT
X	Copyright (c) 1988 by Robert Joseph Silliker
X	All Rights Reserved
X	Permission is granted for unrestricted non-commercial use
X
XSYNOPSIS
X	save_color(handle, flag)
X	int handle, flag;
X
X	do_color(handle)
X	int handle;
X
XDESCRIPTION
X	These library routines provide a means of manipulating the color
X        palette through a dialog box.
X
X	Save_color() is used to  save/restore the  current color pallett
X	settings into/from a global rgb array called old_rgb[][3].  This
X	usually is called at the start of the application with a flag
X	value of 1.  This causes the current settings to be saved.  When
X        the program terminates, this routine should be called with a flag
X        value of 0.  This will cause the current  settings to be restored.
X        Handle is what you get from opening a virtual workstation.
X
X	Do_color() displays a dialog and allows the user to set the RGB
X	of any of the active colors.  The user can cancel the operation
X	which will restore the colors to what they were before do_color()
X	was called.  The default exit button is CANCEL so if the user
X	manages to set foreground and background colors the same pressing
X	the <RETURN> key will cancel the operation.
X
XWARNING
X	Save_color() must be called  with a flag value of 1 before a call
X	is made to do_color().  If you don't then all colors will be set
X	to 0.
SHAR_EOF
chmod 0600 COLOR.DOC || echo "restore of COLOR.DOC fails"
sed 's/^X//' << 'SHAR_EOF' > COLOR.H &&
X#define TSETCOLR 1
X#define COLOR1_2 3
X#define COLOR1_1 2
X#define COLOR1_3 4
X#define COLOR1_4 5
X#define COLOR1_6 7
X#define COLOR1_5 6
X#define COLOR1_7 8
X#define COLOR1_8 9
X#define COLOR2_1 11
X#define COLOR2_7 12
X#define COLOR2_6 13
X#define COLOR2_3 15
X#define COLOR2_4 14
X#define COLOR2_2 16
X#define COLOR2_5 17
X#define COLOR2_8 18
X#define COLOR3_1 20
X#define COLOR3_8 21
X#define COLOR3_6 23
X#define COLOR3_7 22
X#define COLOR3_5 24
X#define COLOR3_4 25
X#define COLOR3_2 27
X#define COLOR3_3 26
X#define ARROWBOX 28
X#define ARROW0 29
X#define ARROW4 31
X#define ARROW2 30
X#define ARROW6 32
X#define ARROW8 33
X#define ARROW12 35
X#define ARROW10 34
X#define ARROW14 36
X#define ARROW1 37
X#define ARROW5 39
X#define ARROW3 38
X#define ARROW7 40
X#define ARROW9 41
X#define ARROW13 43
X#define ARROW11 42
X#define ARROW15 44
X#define COLOROK 48
X#define COLORCAN 49
SHAR_EOF
chmod 0600 COLOR.H || echo "restore of COLOR.H fails"
sed 's/^X//' << 'SHAR_EOF' > s2_temp_.tmp &&
Xbegin 600 COLOR.RSC
XM````+@`N`"X`+@`N`"0`+@`N!-X`,@`"``````````````3F3TL`0T%.0T5,
XM`/__``$`,0`4````$``"$0```````!L`$0`*``(`"0`4``````#_$0``!``!
XM``,`"``#_____P`4`!4````!$0````````,``0`$_____P`4`!4````!$0``
XM```!``,``0`%_____P`4`!4````!$0`````"``,``0`&_____P`4`!4````!
XM$0`````#``,``0`'_____P`4`!4````!$0`````$``,``0`(_____P`4`!4`
XM```!$0`````%``,``0`)_____P`4`!4````!$0`````&``,``0`!_____P`4
XM`!4````!$0`````'``,``0`3``L`$@`4``````#_$0``#``!``,`"``,____
XM_P`4`!4````!$0````````,``0`-_____P`4`!4````!$0`````&``,``0`.
XM_____P`4`!4````!$0`````%``,``0`/_____P`4`!4````!$0`````#``,`
XM`0`0_____P`4`!4````!$0`````"``,``0`1_____P`4`!4````!$0`````!
XM``,``0`2_____P`4`!4````!$0`````$``,``0`*_____P`4`!4````!$0``
XM```'``,``0`<`!0`&P`4``````#_$0``%``!``,`"``5_____P`4`!4````!
XM$0````````,``0`6_____P`4`!4````!$0`````'``,``0`7_____P`4`!4`
XM```!$0`````&``,``0`8_____P`4`!4````!$0`````%``,``0`9_____P`4
XM`!4````!$0`````$``,``0`:_____P`4`!4````!$0`````#``,``0`;____
XM_P`4`!4````!$0`````"``,``0`3_____P`4`!4````!$0`````!``,``0`M
XM`!T`+``4````````$4```0`*`!D`!0`>_____P`4``4```#_$0```0`!``(`
XM`0`?_____P`4``4```#_$7(`!``!``(``0`@_____P`4``4```#_$70`!P`!
XM``(``0`A_____P`4``4```#_$78`"@`!``(``0`B_____P`4``4```#_$7@`
XM#0`!``(``0`C_____P`4``4```#_$7H`$``!``(``0`D_____P`4``4```#_
XM$7P`$P`!``(``0`E_____P`4``4```#_$7X`%@`!``(``0`F_____P`4``4`
XM``#_$7$``0`#``(``0`G_____P`4``4```#_$7,`!``#``(``0`H_____P`4
XM``4```#_$74`!P`#``(``0`I_____P`4``4```#_$7<`"@`#``(``0`J____
XM_P`4``4```#_$7D`#0`#``(``0`K_____P`4``4```#_$7L`$``#``(``0`L
XM_____P`4``4```#_$7T`$P`#``(``0`<_____P`4``4```#_$7\`%@`#``(`
XM`0`N_____P`;`````%+_$0``!``)``,``0`O_____P`;`````$?_$0``#``)
XM``,``0`P_____P`;`````$+_$0``%``)``,``0`Q_____P`:``4``````"0`
XG`@`/``@``0``_____P`:`"<``````"<`$0`/``@``0```"X````N
X`
Xend
SHAR_EOF
uudecode < s2_temp_.tmp && rm -f s2_temp_.tmp &&
chmod 0600 COLOR.RSC || echo "restore of COLOR.RSC fails"
sed 's/^X//' << 'SHAR_EOF' > MAKEFILE &&
X#----------------------------------------------------------------------------
X#		  Makefile to make the library routine 'color.o'
X#----------------------------------------------------------------------------
X
XOBJECT = color.prg
XFILES  = color.o test.o
X
Xcolor.o: color.c objects.c
X
X$(FILES): color.h
X
Xobjects.c: color.rsc
X	\bin\rsctoc.ttp color.rsc -p b_ -o objects.c
X
X$(OBJECT): $(FILES)
X	$(CC) $(FILES) -o $(OBJECT)
X
Xclean:
X	$(RM) $(FILES)
X	$(RM) $(OBJECT)
X
X#----------------------------------------------------------------------------
SHAR_EOF
chmod 0600 MAKEFILE || echo "restore of MAKEFILE fails"
sed 's/^X//' << 'SHAR_EOF' > README &&
XThis is the source for a dialog to play with the color palette.  It was
Xintended that this be an example of how to the the 'rsctoc' program.
XHowever, as distributed the source can be used without 'rsctoc'.  This
Xwas compiled under Laser C and will probably need some mods to work
Xwith another compiler.
X
Xcolor.doc - manual page for the color library routines.
X
Xtest.c    - is a test program that calls the routines provided by color.
X
Xcolor.c   - is the source code that handles the dialog box.
X
Xobjects.c - is the object tree for the color dialog and was generated
X            by running the 'rsctoc' program on the 'color.rsc' file.
X            See the makefile for command line args.
X
Xcolor.rsc - resource file for the color dialog.
X
Xcolor.h   - header file created by the Megamax RCP program.
X
Xcolor.def - another file created by the Megamax RCP program that contains
X            information about the object names and types.
X
Xmakefile  - makefile that I use with my version of make.  This will probably
X            need some changes.
X
X
XBob Silliker
SHAR_EOF
chmod 0600 README || echo "restore of README fails"
sed 's/^X//' << 'SHAR_EOF' > TEST.C &&
X/***************************************************/
X/*  						   */
X/*  			COLOR.C  		   */
X/*  						   */
X/*  Copyright (c) 1988, by Robert Joseph Silliker  */
X/*  		  All Rights Reserved   	   */
X/*  	Permission is granted for unrestricted     */
X/*  		 non-commercial use		   */
X/*  						   */
X/***************************************************/
X
X#include <osbind.h>
X#include <gemdefs.h>
X#include <obdefs.h>
X#include "color.h"
X
Xint intin[128];
Xint intout[128];
Xint ptsin[128];
Xint ptsout[128];
Xint contrl[12];
X
Xmain()
X{
X  int i, handle;
X  int work_in[11],work_out[57];
X
X  appl_init();
X
X  handle = graf_handle(&i, &i, &i, &i);
X
X  for (i = 0; i < 10; ++i)
X    work_in[i] = 1;
X  work_in[10] = 2;
X
X  v_opnvwk(work_in, &handle, work_out);
X
X  graf_mouse(ARROW, 0);
X
X  save_color(handle, 1);
X  do_color(handle);
X  save_color(handle, 0);
X
X  v_clsvwk(handle);
X  appl_exit();
X}
X
X/* END OF FILE */
SHAR_EOF
chmod 0600 TEST.C || echo "restore of TEST.C fails"
exit 0