Colors in Flua?


Forum: Programming and Scripting
Topic: Colors in Flua?
started by: mikshaw

Posted by mikshaw on Jan. 10 2006,03:24
I don't understand the way colors are used in flua.  I can set one of 16 colors with a number, and most of those can also be set with Color.<colorname>.  But flua has support for many more colors than those 16, and i'm finding it difficult to use them with what i'm getting from the documentation.  Even the input.lua script provided in the flua package uses a color picker, but i'm having trouble understanding what it's doing.

Does anyone here have experience with using colors in flua, or can understand the docs well enough to explain the syntax to me?

Thanks for your time.

Posted by roberts on Jan. 10 2006,06:35
Not sure of your question.
I am using the color chooser in my wallpaper.lua to select solid color backgrounds for dsl v2.2. The color choose is pretty easy to use, give it a starting color in decimal for R G B and it will return a new decimal value choosen. Then I convert to hex for xsri. For example the jwm default background that I chose was #336699 so I load up color_chooser with rgb set to 51,102,153 and then let the use choose from there what they would want. The result is a success flag and the three rgb values.

But maybe your question is on get_color and set_color? Those appear to be setting or extracting the rgb values for a color by name or number. Which you could then use by such name or number for the duration of your program.

Posted by mikshaw on Jan. 10 2006,14:50
Yes, it is mostly get_color/set_color that i was messing with...it seems to be the only way to set specific rgb color instead of choosing one of the defaults from the 16-color palette (or using the color picker...i'm looking for a non-interactive way of setting arbitrary colors).  It appears that "color=Color.get_color(n,n,n)" works, but the integers needed do not seem to follow the pattern you'd find in rgb.txt: 0-255 for red, green, and blue respectively.  Also tried Color.gray_ramp(n), but can't get that to work either.

Specifically, this is the sort of thing I'm trying to accomplish.  Say I want my button label text to be equivalent to "orange1", so I try this:
label_color=Color.get_color(255,165,0)
..but it makes something like aquamarine instead.

Maybe i need to first define a color name using rgb values, and then set a color property by that name?

Posted by mikshaw on Jan. 10 2006,16:05
Found one way to do it, after a lot of trial and error...

Instead of setting RGB within the widget properties, I use a number like usual, such as 5, which is magenta by default.  However, at the top of the file i set the color 5 to 255,165,0 to make it orange instead of magenta:
Code Sample
Color:set_color(5,255,165,0)

This also changes Color.magenta, so i assume the color names refer to the color numbers.

Posted by clacker on Jan. 10 2006,18:41
mikshaw, I use other numbers than the named ones and it still works.  I don't know how many entries I can make into the color table, but it looks like color numbers below 1001 work but funny things happen after 1024.  Here is my example (using your orange):
Code Sample
-- set up four colors
-- color table goes up to "small number" according to docs

temp = Color:set_color(17,255,165,0)
temp = Color:set_color(124,125,125,256)
temp = Color:set_color(254,25,25,125)
temp = Color:set_color(1001,25,125,125)

w = Window{320,200,"Test Window"}
w.color = 17

name = Input{76,10,210,25,"Name:"}
name.color = 124

myBox = Box{20,40,100,100}
myBox.color = 254
myBox.box = Boxtype.flat

myBox = Box{140,40,60,60}
myBox.color = 1001
myBox.box = Boxtype.flat

w:end_layout()
w:show()

Posted by mikshaw on Jan. 10 2006,18:56
oooooh...that's exactly what i needed, thank you.
I noticed there was a way to add colors to the color table, but couldn't figure out what to use as a name.
Tried "orange = Color:set_color(Color.orange,255,165,0)", and of course that didn't work.

Posted by clacker on Jan. 10 2006,19:06
I was surprised how many colors you can have at once:

Code Sample
-- set up four colors
-- color table goes up to 255 slots?

temp = Color:set_color(17,255,165,0)
temp = Color:set_color(224,125,125,256)
temp = Color:set_color(258,25,25,125)
temp = Color:set_color(1001,25,125,125)

-- create a set of entries into the color table
-- 101 to 136 as a ramp of blue/red
-- 137 to 172 as a ramp of blue/green
for i = 1,36 do
   temp = Color:set_color(100+i,255-7*i,0,7*i)
end

for i = 1,36 do
   temp = Color:set_color(136+i,0,255-7*i,7*i)
end

w = Window{320,200,"Test Window"}
w.color = 17

--make a bunch of colored boxes
for i = 1,36 do
   myBox = Box{10*i,150,5,5}
   myBox.color = 100+i
   myBox.box = Boxtype.flat
end

for i = 1,36 do
   myBox = Box{10*i,160,5,5}
   myBox.color = 136+i
   myBox.box = Boxtype.flat
end

name = Input{76,10,210,25,"Name:"}
name.color = 224

myBox = Box{20,40,100,100}
myBox.color = Color.white
myBox.box = Boxtype.flat

myBox = Box{140,40,60,60}
myBox.color = 1001
myBox.box = Boxtype.flat

w:end_layout()
w:show()


You can also add your own color names for the numbers in the Color table:

Code Sample
temp = Color:set_color(17,255,165,0)
Color.orange = 17

myBox = Box{140,40,60,60}
myBox.color = Color.orange
myBox.box = Boxtype.flat

Posted by mikshaw on Jan. 10 2006,21:21
Here's my new colors reference widget.  Thank you for your help.

Code Sample
#!/bin/flua

-- Color codes for Lua FLTK
-- mikshaw 2005,2006
-- thanks to clacker and roberts for help

-- keep slider colors updated as values change
function update_sliders()
Color:set_color(100,slideR.value,slideG.value,slideB.value)
slideR.selection_color = 100
slideG.selection_color = 100
slideB.selection_color = 100
slideR:redraw();slideG:redraw();slideB:redraw()
end

-- default widget properties
Fl_Widget.initializers = {textfont = 15, labelfont = 15,box=Boxtype.thin_up}
Fl_Slider.initializers = {type=Slidertype.hor,align=Align.left,step=1,minimum=0,maximum=255,selection_color=0,callback=update_sliders}
Fl_Box.initializers = {align=Align.right}

-- MAIN WINDOW
ww = 480
wh = 365
w = Window{ww,wh,"Lua FLTK Colors"}

-- make a pretty window border
frame = Box{1,1,ww-2,wh-2; box=Boxtype.engraved}

-- draw a column of numbered colors, 0-15
colors = {}
for i = 0,15 do
colors[i] = Box{30,15+i*20,80,20,tostring(i);color=i}
end

-- draw a column of named colors
cblack = Box{140,15,80,20,"black";color=Color.black}
cred = Box{140,35,80,20,"red";color=Color.red}
cgreen = Box{140,55,80,20,"green";color=Color.green}
cyellow = Box{140,75,80,20,"yellow";color=Color.yellow}
cblue = Box{140,95,80,20,"blue";color=Color.blue}
cmagenta = Box{140,115,80,20,"magenta";color=Color.magenta}
ccyan = Box{140,135,80,20,"cyan";color=Color.cyan}
cwhite = Box{140,155,80,20,"white";color=Color.white}
cselection = Box{140,315,80,20,"selection";color=Color.selection}
cgray = Box{140,335,80,20,"gray (default)";color=Color.gray}

-- add a new color to the color table (orange)
colors[16] = Color:set_color(16,255,165,0)
new_color = Box{300,35,160,160,"New color (16): ";color=16,align=Align.top}

-- dropdown list of numbered colors
retrieve_color = Choice{300,215,160,20;when=When.release + When.not_changed}
for i = 0,16 do
retrieve_color:add(Menu_Entry{"Get Color "..i})
end
retrieve_color.value = 0
function retrieve_color.callback()
slideR.value,slideG.value,slideB.value = Color:get_color(retrieve_color.value)
update_sliders()
end

-- color sliders, value 0-255 (see Fl_Slider.initializers for properties)
slideR = Value_Slider{300,235,160,20,"R"}
slideG = Value_Slider{300,255,160,20,"G"}
slideB = Value_Slider{300,275,160,20,"B"}

-- changes the color of new color 16 and redraws it
change_color = Button{300,295,160,20,"Apply to color 16"}
function change_color.callback()
Color:set_color(16,slideR.value,slideG.value,slideB.value)
new_color:redraw()
end

w:end_layout()
w:show()


Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.