2 // "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Color chooser test program for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
29 #include <FL/Fl_Window.H>
30 #include <FL/Fl_Box.H>
31 #include <FL/Fl_Button.H>
32 #include <FL/fl_show_colormap.H>
33 #include <FL/Fl_Color_Chooser.H>
34 #include <FL/Fl_Image.H>
36 #include <FL/fl_draw.H>
40 #if !defined(WIN32) && !defined(__APPLE__)
41 #include "list_visuals.cxx"
49 image
= new uchar
[3*width
*height
];
51 for (int y
= 0; y
< height
; y
++) {
52 double Y
= double(y
)/(height
-1);
53 for (int x
= 0; x
< width
; x
++) {
54 double X
= double(x
)/(width
-1);
55 *p
++ = uchar(255*((1-X
)*(1-Y
))); // red in upper-left
56 *p
++ = uchar(255*((1-X
)*Y
)); // green in lower-left
57 *p
++ = uchar(255*(X
*Y
)); // blue in lower-right
62 class Pens
: public Fl_Box
{
65 Pens(int X
, int Y
, int W
, int H
, const char* L
)
66 : Fl_Box(X
,Y
,W
,H
,L
) {}
69 // use every color in the gray ramp:
70 for (int i
= 0; i
< 3*8; i
++) {
71 fl_color((Fl_Color
)(FL_GRAY_RAMP
+i
));
72 fl_line(x()+i
, y(), x()+i
, y()+h());
77 #define fullcolor_cell (FL_FREE_COLOR)
79 void cb1(Fl_Widget
*, void *v
) {
80 c
= fl_show_colormap(c
);
81 Fl_Box
* b
= (Fl_Box
*)v
;
83 b
->parent()->redraw();
86 void cb2(Fl_Widget
*, void *v
) {
88 Fl::get_color(c
,r
,g
,b
);
89 if (!fl_color_chooser("New color:",r
,g
,b
,3)) return;
91 Fl::set_color(fullcolor_cell
,r
,g
,b
);
92 Fl_Box
* bx
= (Fl_Box
*)v
;
93 bx
->color(fullcolor_cell
);
94 bx
->parent()->redraw();
97 int main(int argc
, char ** argv
) {
98 Fl::set_color(fullcolor_cell
,145,159,170);
99 Fl_Window
window(400,400);
100 Fl_Box
box(50,50,300,300);
101 box
.box(FL_THIN_DOWN_BOX
);
104 Fl_Button
b1(140,120,120,30,"fl_show_colormap");
105 b1
.callback(cb1
,&box
);
106 Fl_Button
b2(140,160,120,30,"fl_choose_color");
107 b2
.callback(cb2
,&box
);
108 Fl_Box
image_box(140,200,120,120,0);
110 (new Fl_RGB_Image(image
, width
, height
))->label(&image_box
);
111 Fl_Box
b(140,320,120,0,"Example of fl_draw_image()");
112 Pens
p(80,200,3*8,120,"lines");
113 p
.align(FL_ALIGN_TOP
);
115 if (!Fl::args(argc
,argv
,i
) || i
< argc
-1) {
116 printf("usage: %s <switches> visual-number\n"
117 " - : default visual\n"
118 " r : call Fl::visual(FL_RGB)\n"
119 " c : call Fl::own_colormap()\n",argv
[0]);
120 #if !defined(WIN32) && !defined(__APPLE__)
121 printf(" # : use this visual with an empty colormap:\n");
128 if (argv
[i
][0] == 'r') {
129 if (!Fl::visual(FL_RGB
)) printf("Fl::visual(FL_RGB) returned false.\n");
130 } else if (argv
[i
][0] == 'c') {
132 } else if (argv
[i
][0] != '-') {
133 #if !defined(WIN32) && !defined(__APPLE__)
134 int visid
= atoi(argv
[i
]);
136 XVisualInfo templt
; int num
;
137 templt
.visualid
= visid
;
138 fl_visual
= XGetVisualInfo(fl_display
, VisualIDMask
, &templt
, &num
);
139 if (!fl_visual
) Fl::fatal("No visual with id %d",visid
);
140 fl_colormap
= XCreateColormap(fl_display
, RootWindow(fl_display
,fl_screen
),
141 fl_visual
->visual
, AllocNone
);
142 fl_xpixel(FL_BLACK
); // make sure black is allocated
144 Fl::fatal("Visual id's not supported on MSWindows or MacOS.");
148 window
.show(argc
,argv
);
153 // End of "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $".