I wrote:
>> So I've incorporated the fl_draw() call in an old example that I
>> posted several years ago. Basically I want something simple like:
>>
>>     char buffer[32];
>>     sprintf(buffer, "%d", value);
>>     fl_draw(buffer, x() + 30, y() + 30);

Greg replied:
>       Don't forget to set the font ;) eg. fl_font().
>       FLTK will crash if the font isn't set before the fl_draw() call.

Doh! I *knew* it would be something simple. I didn't think about
calling fl_font() first because I already had text on the screen
inside button widgets, etc. so assumed it would use the same font.

I worked around it yesterday by adding a group above a similar
colour bar and using Fl_Box widgets and their labels to show the
min/max values. Anyway, I include the updated example below for
those who are interested.

Oh, and I'm still interested to know whether there are "standard"
ways of calculating such a colour/temperature spectrum.

Cheers
D.

/*
  While trying to find a stupid error (Doh! max uchar is 255 and not 256!)
  I've written a simple fl_color demo that someone might find useful.
*/

// Just compile using 'fltk-config --compile filename.cxx'
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>

#include <cmath>
#include <cstdio>
#include <cstring>

void
setGrey (double ratio)
{
  uchar grey = (uchar) (255 * ratio);
  fl_color (grey);
}

void
setBlueToCyan (double ratio)
{
  uchar red = 0;
  uchar green = (uchar) (255 * ratio);
  uchar blue = 255;
  fl_color (red, green, blue);
}

void
setCyanToGreen (double ratio)
{
  uchar red = 0;
  uchar green = 255;
  uchar blue = (uchar) (255 * (1.0 - ratio));
  fl_color (red, green, blue);
}

void
setGreenToYellow (double ratio)
{
  uchar red = (uchar) (255 * ratio);
  uchar green = 255;
  uchar blue = 0;
  fl_color (red, green, blue);
}

void
setYellowToRed (double ratio)
{
  uchar red = 255;
  uchar green = (uchar) (255 * (1.0 - ratio));
  uchar blue = 0;
  fl_color (red, green, blue);
}

void
setColor (double min, double max, double value)
{
  double range = max - min;
  double delta = value - min;
  double ratio = delta / range;

  if (ratio <= 0.0) {
      setGrey (0.3);
  }
  else if (ratio < 0.25) {
      setBlueToCyan (4.0 * (ratio - 0.00));
  }
  else if (ratio < 0.50) {
      setCyanToGreen (4.0 * (ratio - 0.25));
  }
  else if (ratio < 0.75) {
      setGreenToYellow (4.0 * (ratio - 0.50));
  }
  else if (ratio < 1.00) {
      setYellowToRed (4.0 * (ratio - 0.75));
  }
  else {
      setGrey (0.7);
  }
}

void
drawCircle (int x, int y, int r)
{
  fl_pie (x - r, y - r, 2 * r, 2 * r, 0.0, 360.0);
  fl_color (FL_BLACK);
  fl_arc (x - r, y - r, 2 * r, 2 * r, 0.0, 360.0);
}

class Box:public Fl_Box
{
public:

  Box (int x, int y, int w, int h, const char *t = 0):Fl_Box (x, y, w, h, t)
  {
  }

   ~Box ()
  {
  }

  void draw ()
  {
    int xc = x () + w () / 2;
    int yc = y () + h () / 2;
    int rc = (w () < h ())? w () / 3 : h () / 3;

    drawCircle (xc, yc, rc);
    for (double angle = 0.0; angle < 360.0; angle += 10.0) {
        int xx = (int) (xc + rc * sin (angle * M_PI / 180.0));
        int yy = (int) (yc - rc * cos (angle * M_PI / 180.0));
        setColor (0.0, 360.0, angle);
        drawCircle (xx, yy, rc / 10);
    }

    for (int i = 0; i < w (); ++i) {
        setColor (0, w (), i);
        fl_line (x () + i, y (), x () + i, y () + 20);
    }

    fl_font(FL_HELVETICA, 12);

    int tw, th;
    char buffer[32];

    setColor(0, w(), 1);
    sprintf(buffer, "%d", 1);
    fl_measure(buffer, tw, th);
    fl_draw(buffer, x(), y()+20+th);

    setColor(0, w(), w()-1);
    sprintf(buffer, "%d", w()-1);
    fl_measure(buffer, tw, th);
    fl_draw(buffer, x()+w()-tw, y()+20+th);
  }
};

int
main (int argc, char *argv[])
{
  Fl_Window *w = new Fl_Window (300, 300);
  Box *b = new Box (5, 5, 290, 290);
  w->end ();

  w->show (argc, argv);
  Fl::run ();
  return (0);
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to