I already opened a thread on this topic ("Could FLTK manage color 
transparency?") and Ian tried to help me, but, maybe for my bad and confuse 
English, it stopped immediately.
So I retry and hope to be more precise:
1) Is there a way to use transparent (alpha-blended) colors in a 
Fl_Widget::draw() method? I read in the doc ("Drawing things in FLTK"):

"FLTK manages colors as 32-bit unsigned integers. Values from 0 to 255 
represent colors from the FLTK 1.0.x standard colormap ...
Color values greater than 255 are treated as 24-bit RGB values..."

Then, if I understood, I can choose between a (solid) color-mapped  or a RGB 
color (still solid: the fourth byte is ignored) and I can not, for example, get 
 a Fl_Box with a semi-transparent background color.
Is this correct?
2) I was able to get semi-transparent rectangles using Fl_RGB_Image class (see 
code below). It works, but there is much flickering with big rectangles.
Then I ask: is there a better way to achieve the same result (without, of 
course, wasting memory with a 1600 x 1200 buffer ...)?
Thanks N. Cassetta

//*********************************************************

#include <cmath>
#include <FL/Fl.H>
#include <Fl/Fl_Box.H>
#include <FL/Fl_Overlay_Window.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_draw.H>

// **************** the translucent box

const Fl_Color ALPHA_COLOR = (Fl_Color)0x80400080;

class SelectionRect : public Fl_RGB_Image {
    public:
                            SelectionRect(int W, int H, Fl_Color c = 
ALPHA_COLOR);
                    // W and H are the buffer dimensionse
        void                draw(int X, int Y, int W, int H);
    private:
        Fl_Color            col;
        unsigned char*      buffer;
};

SelectionRect::SelectionRect(int W, int H, Fl_Color c) :
    Fl_RGB_Image(buffer = new unsigned char[4*W*H], W, H, 4), col(c) {
    int len = W * H;
    Fl_Color* p = (Fl_Color *)buffer;
    for (int i = 0; i < len; i++, p++)      //fills the buffer
        *p = col;
}

void SelectionRect::draw(int X, int Y, int W, int H) {
// this is like Fl_Tiled_Image drawing
    fl_push_clip(X, Y, W, H);
    int xtimes = W / w();
    int ytimes = H / h();
    for(int j = 0; j <= ytimes; j++)
        for (int i = 0; i <= xtimes; i++)
            Fl_RGB_Image::draw(X + i * w(), Y + j * h());
    fl_rect(X, Y, W, H);
    fl_pop_clip();
}


// ************** the main window

class MainWindow : public Fl_Overlay_Window {
    public:
                        MainWindow();
        virtual void    draw_overlay();
        virtual int     handle(int e);
    private:
        int min(int a,int b)    { return a < b ? a : b ; }
        Fl_Box*         MyBox;
        SelectionRect*  SelRect;
        int             xbegin, ybegin, xend, yend;
};

MainWindow::MainWindow(): Fl_Overlay_Window(100, 100, 600, 300) {
    MyBox = new Fl_Box(100, 50, 400, 200, "Try to click and draw!");
        // only for demonstrate trasparency
    MyBox->box(FL_UP_BOX);
    MyBox->align(FL_ALIGN_INSIDE | FL_ALIGN_WRAP);
    MyBox->labelsize(60);
    end();
    SelRect = new SelectionRect(100, 100);
};

void MainWindow::draw_overlay() {
    int x = min(xbegin, xend), y = min(ybegin, yend);
    int w = fabs(xbegin - xend), h = fabs(ybegin - yend);
    SelRect->draw(x, y, w, h);
}

int MainWindow::handle(int e) {
    switch(e) {
        case FL_PUSH:
            xbegin = xend = Fl::event_x();
            ybegin = yend = Fl::event_y();
            break;
        case FL_DRAG:
            xend = Fl::event_x();
            yend = Fl::event_y();
            break;
        case FL_RELEASE:
            break;
        default:
            return 0;
    }
    redraw_overlay();
    return 1;
}

// ************************** the main()

int main()
{
    MainWindow* mwin = new MainWindow ();
    mwin->show();
    return Fl::run();
}

_______________________________________________
fltk mailing list
fltk@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to