Volkan Ozyilmaz
Volkan Ozyilmaz

Reputation: 500

fltk setting button's active label color

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.

Upvotes: 0

Views: 2415

Answers (3)

mds
mds

Reputation: 129

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.

Upvotes: 0

cup
cup

Reputation: 8257

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");
    

    ...

Upvotes: 2

cup
cup

Reputation: 8257

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();
}

Upvotes: 0

Related Questions