You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.5KB

  1. //
  2. // "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Color chooser test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  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.
  12. //
  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.
  17. //
  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
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Window.H>
  29. #include <FL/Fl_Box.H>
  30. #include <FL/Fl_Button.H>
  31. #include <FL/fl_show_colormap.H>
  32. #include <FL/Fl_Color_Chooser.H>
  33. #include <FL/Fl_Image.H>
  34. #include <FL/x.H>
  35. #include <FL/fl_draw.H>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #if !defined(WIN32) && !defined(__APPLE__)
  39. #include "list_visuals.cxx"
  40. #endif
  41. int width = 75;
  42. int height = 75;
  43. uchar *image;
  44. void make_image() {
  45. image = new uchar[3*width*height];
  46. uchar *p = image;
  47. for (int y = 0; y < height; y++) {
  48. double Y = double(y)/(height-1);
  49. for (int x = 0; x < width; x++) {
  50. double X = double(x)/(width-1);
  51. *p++ = uchar(255*((1-X)*(1-Y))); // red in upper-left
  52. *p++ = uchar(255*((1-X)*Y)); // green in lower-left
  53. *p++ = uchar(255*(X*Y)); // blue in lower-right
  54. }
  55. }
  56. }
  57. class Pens : public Fl_Box {
  58. void draw();
  59. public:
  60. Pens(int X, int Y, int W, int H, const char* L)
  61. : Fl_Box(X,Y,W,H,L) {}
  62. };
  63. void Pens::draw() {
  64. // use every color in the gray ramp:
  65. for (int i = 0; i < 3*8; i++) {
  66. fl_color((Fl_Color)(FL_GRAY_RAMP+i));
  67. fl_line(x()+i, y(), x()+i, y()+h());
  68. }
  69. }
  70. Fl_Color c = FL_GRAY;
  71. #define fullcolor_cell (FL_FREE_COLOR)
  72. void cb1(Fl_Widget *, void *v) {
  73. c = fl_show_colormap(c);
  74. Fl_Box* b = (Fl_Box*)v;
  75. b->color(c);
  76. b->parent()->redraw();
  77. }
  78. void cb2(Fl_Widget *, void *v) {
  79. uchar r,g,b;
  80. Fl::get_color(c,r,g,b);
  81. if (!fl_color_chooser("New color:",r,g,b,3)) return;
  82. c = fullcolor_cell;
  83. Fl::set_color(fullcolor_cell,r,g,b);
  84. Fl_Box* bx = (Fl_Box*)v;
  85. bx->color(fullcolor_cell);
  86. bx->parent()->redraw();
  87. }
  88. int main(int argc, char ** argv) {
  89. Fl::set_color(fullcolor_cell,145,159,170);
  90. Fl_Window window(400,400);
  91. Fl_Box box(50,50,300,300);
  92. box.box(FL_THIN_DOWN_BOX);
  93. c = fullcolor_cell;
  94. box.color(c);
  95. Fl_Button b1(140,120,120,30,"fl_show_colormap");
  96. b1.callback(cb1,&box);
  97. Fl_Button b2(140,160,120,30,"fl_choose_color");
  98. b2.callback(cb2,&box);
  99. Fl_Box image_box(140,200,120,120,0);
  100. make_image();
  101. (new Fl_RGB_Image(image, width, height))->label(&image_box);
  102. Fl_Box b(140,320,120,0,"Example of fl_draw_image()");
  103. Pens p(80,200,3*8,120,"lines");
  104. p.align(FL_ALIGN_TOP);
  105. int i = 1;
  106. if (!Fl::args(argc,argv,i) || i < argc-1) {
  107. printf("usage: %s <switches> visual-number\n"
  108. " - : default visual\n"
  109. " r : call Fl::visual(FL_RGB)\n"
  110. " c : call Fl::own_colormap()\n",argv[0]);
  111. #if !defined(WIN32) && !defined(__APPLE__)
  112. printf(" # : use this visual with an empty colormap:\n");
  113. list_visuals();
  114. #endif
  115. puts(Fl::help);
  116. exit(1);
  117. }
  118. if (i!=argc) {
  119. if (argv[i][0] == 'r') {
  120. if (!Fl::visual(FL_RGB)) printf("Fl::visual(FL_RGB) returned false.\n");
  121. } else if (argv[i][0] == 'c') {
  122. Fl::own_colormap();
  123. } else if (argv[i][0] != '-') {
  124. #if !defined(WIN32) && !defined(__APPLE__)
  125. int visid = atoi(argv[i]);
  126. fl_open_display();
  127. XVisualInfo templt; int num;
  128. templt.visualid = visid;
  129. fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
  130. if (!fl_visual) Fl::fatal("No visual with id %d",visid);
  131. fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
  132. fl_visual->visual, AllocNone);
  133. fl_xpixel(FL_BLACK); // make sure black is allocated
  134. #else
  135. Fl::fatal("Visual id's not supported on MSWindows or MacOS.");
  136. #endif
  137. }
  138. }
  139. window.show(argc,argv);
  140. return Fl::run();
  141. }
  142. //
  143. // End of "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $".
  144. //