0

I am using fltk 1.3.2. I set the button's label color with

_button->labelcolor(fl_rgb_color(162, 60, 62));

but when I press the button, the color is changed.

I couldn't find the function how set the active label color.

Does anyone know how to do that?

Edit: I use Fl::background() and Fl::foreground() functions before creating the window. This make the problem.

Edit2:

This example shows the problem.

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <iostream>

void HitMe(Fl_Widget* w)
{
    std::cout << "Ouch" << std::endl;
}

int main(int argc, char ** argv) 
{
  Fl::background(0x60, 0x66, 0x60);

  Fl_Window *window = new Fl_Window(320,130);
  Fl_Button *b = new Fl_Button(10, 10, 130, 30, "A red label");
  b->labelcolor(fl_rgb_color(162, 60, 20));
  b->callback(HitMe);
  window->end();
  window->show(argc,argv);
  return Fl::run();
}

When I commented out Fl::background() function everything is alright.

Volkan Ozyilmaz
  • 470
  • 4
  • 19

3 Answers3

2

What you are seeing is the contrasting colour (see commented out code below). FLTK does this when the button is pressed. It gets the colour of the button, works out the contrasting colour based on the foreground and background colours. Have a look at the help for fl_contrast for more details.

Basically, if there is enough contrast, it will use the foreground colour otherwise it will find a contrasting colour for your background.

What can you do about it?

  1. do nothing - that is how it is
  2. choose a lighter background colour which will satisfy the contrast conditions
  3. make your own button type with its own draw method

    class KeepFGButton : public Fl_Button { public: KeepFGButton(int x, int y, int w, int h, const char* s) : Fl_Button(x, y, w, h, s) { }

    void draw() {
        if (type() == FL_HIDDEN_BUTTON) return;
        Fl_Color col = value() ? selection_color() : color();
        draw_box(value() ? (down_box() ? down_box() : fl_down(box())) : box(), col);
        draw_backdrop();
    
        // Remove the code that changes the contrast
        //if (labeltype() == FL_NORMAL_LABEL && value()) {
        //  Fl_Color c = labelcolor();
        //  labelcolor(fl_contrast(c, col));
        //  draw_label();
        //  labelcolor(c);
        //}
        //else
            draw_label();
        if (Fl::focus() == this) draw_focus();
    }
    

    };

    int main(int argc, char ** argv) { Fl::background(0x60, 0x66, 0x60);

    Fl_Window *window = new Fl_Window(320, 130);
    Fl_Button *b = new KeepFGButton(10, 10, 130, 30, "A red label");
    

    ...

cup
  • 7,589
  • 4
  • 19
  • 42
  • Thank you @cup, I hope in future versions of fltk this can be made easier. I will choose different color to solve the problem, I don't want to struggle with this. – Volkan Ozyilmaz Sep 29 '14 at 12:03
0

Try the following and let me know if, when you run it, the label becomes white. If it doesn't then there is possibly something else that you are doing that is not quite right. If it does, I don't know what the problem is.

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <iostream>

void HitMe(Fl_Widget* w)
{
    std::cout << "Ouch" << std::endl;
}

int main(int argc, char ** argv) {
  Fl_Window *window = new Fl_Window(320,130);
  Fl_Button *b = new Fl_Button(10, 10, 130, 30, "A red label");
  b->labelcolor(fl_rgb_color(162, 60, 20));
  b->callback(HitMe);
  window->end();
  window->show(argc,argv);
  return Fl::run();
}
cup
  • 7,589
  • 4
  • 19
  • 42
  • your code is working well. The button label is red. Afterward I add my code's difference (I add b->box(FL_NO_BOX) and b->down_box(FL_NO_BOX) functions) and it is also working well. Finally I found the guilty. I use Fl::background(0x60, 0x66, 0x60); and Fl::foreground(0xAC, 0xAC, 0xAC); this Fl::background make it wrong. Could you try with Fl:background() in beginning of the code? – Volkan Ozyilmaz Sep 27 '14 at 10:46
  • Can you just post the code instead of describing it. It is difficult to tell from the description, which part comes first and which part comes next. – cup Sep 27 '14 at 18:10
  • Sorry, ... int main(int argc, char ** argv) { Fl::background(0x60, 0x66, 0x60); Fl::foreground(0xAC, 0xAC, 0xAC); Fl_Window *window = new Fl_Window(320, 130); ... – Volkan Ozyilmaz Sep 28 '14 at 05:49
  • Edit your original question and post it there. Comments aren't very good for posting code – cup Sep 28 '14 at 06:08
0

This is definitely too late to help the author, but in the event that this helps anyone else searching for an answer to this still relevant issue in FLTK, I'm going to offer my solution:

If you dig through the source code for FLTK 1.3.4-2 (current stable as of this post), there are a couple of built-in colormap indices which are referenced when the drawing of shadow boxes or frames are called: FL_DARK3 (for the boxes) and FL_DARK2 (for the scrollbar back color in Fl_Scroll). You can look at this FLTK documentation to see how to reset these colors to anything you wish at runtime. In particular, anticipating that this ugly red default mess will show up whenever there's a sufficiently dark background, it works well for me to just set these to a slightly lighter version of the overlayed box color:

    Fl_Color boxColor = 0x88888800;
    Fl_Color boxShadowColor = 0xaaaaaa00;
    Fl::set_color(FL_DARK3, boxShadowColor);
    Fl::set_color(FL_DARK2, boxShadowColor);

Now create your label as above and the display will be free of the red shadow. N.b. it is also possible to override the standard background2() behavior which resets FL_BACKGROUND_COLOR to the one produced by fl_contrast:

    Fl::set_color(FL_BACKGROUND2_COLOR, yourBgColor);

The same trick works for other hard to reset colors like FL_INACTIVE_COLOR and FL_SELECTION_COLOR.

Hope this workaround helps.

mds
  • 129
  • 4