Re: [Vala] Convert RGB color value to HEX



2012/9/3 Satyajit Sahoo <satyajit happy gmail com>

Hi. I need to get color value in HEX in my vala program.

Basically, I get a color expression like this,

rgb(red, green, blue) or rgba(red, green, blue, alpha)

But I need to convert it into a format #rrggbb

I cannot seem to understand how to do it.



using Gtk;
using Gdk;

void main(string[] args)
{
    Gtk.init(ref args);

    var win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
    win.destroy.connect(Gtk.main_quit);

    var b = new ColorButton();
    b.use_alpha = true;
    b.color_set.connect( (b) =>
    {
        Gdk.RGBA c = b.get_rgba();
        string s =
            "#%02x%02x%02x%02x"
            .printf((uint)(Math.round(c.red*255)),
                    (uint)(Math.round(c.green*255)),
                    (uint)(Math.round(c.blue*255)),
                    (uint)(Math.round(c.alpha*255)))
            .up();
        stdout.printf("%s\n", s);
    });

    win.add(b);
    win.show_all();

    Gtk.main();
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]