Update of /cvsroot/pure-data/externals/hcs
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3126
Added Files:
colorpanel.c colorpanel-help.pd
Log Message:
working version of the colorpanel
--- NEW FILE: colorpanel-help.pd ---
#N canvas 274 159 494 344 10;
#X obj 99 139 colorpanel;
#X msg 99 56 bang;
#X obj 112 198 pddp/print;
#X obj 98 226 unpack float float float;
#X obj 30 278 nbx 8 20 -1e+37 1e+37 0 0 empty empty red 0 28 0 14 -261234
-1 -1 0.937255 256;
#X obj 150 278 nbx 8 20 -1e+37 1e+37 0 0 empty empty green 0 28 0 14
-204786 -1 -1 0.819608 256;
#X obj 270 278 nbx 8 20 -1e+37 1e+37 0 0 empty empty blue 0 28 0 14
-203904 -1 -1 0.329412 256;
#X text 35 16 pop up a color picker and output the red \, green \,
blue values as a list of three floats ranging from 0 to 1;
#X text 129 177 the list:;
#X text 202 78 or open the window with an initial color;
#X msg 108 78 symbol #beefee;
#X msg 160 107 0.5 0.5 0.5;
#X connect 0 0 2 0;
#X connect 0 0 3 0;
#X connect 1 0 0 0;
#X connect 3 0 4 0;
#X connect 3 1 5 0;
#X connect 3 2 6 0;
#X connect 10 0 0 0;
#X connect 11 0 0 0;
--- NEW FILE: colorpanel.c ---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <m_pd.h>
static t_class *colorpanel_class;
typedef struct _colorpanel
{
t_object x_obj;
t_symbol *x_s;
char current_color[MAXPDSTRING];
} t_colorpanel;
static void colorpanel_bang(t_colorpanel *x)
{
post("pd [concat %s callback [tk_colorpanel -initialcolor %s] \\;]\n",
x->x_s->s_name, x->current_color);
sys_vgui("pd [concat %s callback [tk_colorpanel -initialcolor %s] \\;]\n",
x->x_s->s_name, x->current_color);
}
static void colorpanel_symbol(t_colorpanel *x, t_symbol *s)
{
post("setting initial color: %s", s->s_name);
strncpy(x->current_color, s->s_name, MAXPDSTRING);
colorpanel_bang(x);
}
static void colorpanel_list(t_colorpanel *x, t_symbol *s, int argc, t_atom *argv)
{
t_symbol *tmp_symbol = s; /* <-- this gets rid of the unused variable warning */
int i;
unsigned int tmp_int;
char color_buffer[3];
char color_string[MAXPDSTRING];
strncpy(color_string,"#",MAXPDSTRING);
if(argc > 2) post("[colorpanel] warning more than three elements in list");
for(i=0; i<3; i++)
{
tmp_symbol = atom_getsymbolarg(i, argc, argv);
if(tmp_symbol == &s_)
{
tmp_int = (unsigned int)(atom_getfloatarg(i, argc , argv) * 255);
snprintf(color_buffer, 3, "%02x", (tmp_int > 255 ? 255 : tmp_int));
strncat(color_string, color_buffer, 3);
}
else
{
pd_error(x,"[colorpanel] symbol atom in color list");
}
}
memcpy(x->current_color, color_string, 7);
post("setting initial color: %s", x->current_color);
colorpanel_bang(x);
}
static void colorpanel_callback(t_colorpanel *x, t_symbol *color)
{
t_atom output_atoms[3];
unsigned int red, green, blue;
if(color != &s_)
{
post("callback color: %s", color->s_name);
strncpy(x->current_color, color->s_name, MAXPDSTRING);
sscanf(x->current_color, "#%02x%02x%02x", &red, &green, &blue);
post("current color: %s", x->current_color);
SETFLOAT(output_atoms, (t_float) red / 255);
SETFLOAT(output_atoms + 1, (t_float) green / 255);
SETFLOAT(output_atoms + 2, (t_float) blue / 255);
outlet_list(x->x_obj.ob_outlet, &s_list, 3, output_atoms);
}
}
static void colorpanel_free(t_colorpanel *x)
{
pd_unbind(&x->x_obj.ob_pd, x->x_s);
}
static void *colorpanel_new( void)
{
char buf[MAXPDSTRING];
t_colorpanel *x = (t_colorpanel *)pd_new(colorpanel_class);
sprintf(buf, "#%lx", (t_int)x);
x->x_s = gensym(buf);
pd_bind(&x->x_obj.ob_pd, x->x_s);
outlet_new(&x->x_obj, &s_list);
strcpy(x->current_color,"#ffffff");
return(x);
}
void colorpanel_setup(void)
{
colorpanel_class = class_new(gensym("colorpanel"),
(t_newmethod)colorpanel_new, (t_method)colorpanel_free,
sizeof(t_colorpanel), 0, 0);
class_addbang(colorpanel_class, (t_method)colorpanel_bang);
class_addsymbol(colorpanel_class, (t_method)colorpanel_symbol);
class_addlist(colorpanel_class, (t_method)colorpanel_list);
class_addmethod(colorpanel_class, (t_method)colorpanel_callback,
gensym("callback"), A_DEFSYMBOL, 0);
}