#!/usr/bin/ruby # vim: set ts=8 sw=4 noet require 'gtk2' require 'libglade2' class MainProgram @gxml # GladeXML for the user interface. @sliders # Sliders in user interface. @labels # Labels in user interface. @drawing # Gtk::DrawingArea to fill with pretty colors. @colors # Array of colors (r, g, b) # Creates GUI from glade XML file, and initializes class members. # Also it initiates Gtk (and goes into the Gtk main loop when done), # seeds the random number generator, and sets the color to black. def initialize() Gtk.init srand() @colors = [0, 0, 0] # Parse glade file. @gxml = GladeXML.new("color-test.glade") { |handler_name| method(handler_name) } # Get widgets from the XML object. @sliders = [ @gxml.get_widget("slider_red"), @gxml.get_widget("slider_green"), @gxml.get_widget("slider_blue") ] @labels = [ @gxml.get_widget("label_red"), @gxml.get_widget("label_green"), @gxml.get_widget("label_blue") ] @drawing = @gxml.get_widget("drawingarea") # Enter main loop. Gtk.main end # Updates foreground color for Gtk::DrawingArea based on the values in # @colors, where @colors[0] is the amount of red, @colors[1] is the # amount of green, and @colors[2] is the amount of blue. def update_drawing_color() color = Gdk::Color.new(@colors[0] * 255, @colors[1] * 255, @colors[2] * 255) @drawing.modify_fg(@drawing.state, color) end # Handles termination of the window (quits the Gtk main loop). def on_window_delete_event() Gtk.main_quit end # Generates random values for red, green and blue, and set the values in # the corresponding sliders. def on_button_random_clicked() # Genrate new values. @colors = [rand(255), rand(255), rand(255)] # Set valus for sliders. @sliders[0].value = @colors[0] @sliders[1].value = @colors[1] @sliders[2].value = @colors[2] end # Sets the red color to the value of the slider, and updates the # corresponding label and the drawing area. def on_slider_red_value_changed() @colors[0] = @sliders[0].value.to_i @labels[0].text = "R: " + @sliders[0].value.to_i.to_s update_drawing_color() end # Sets the green color to the value of the slider, and updates the # corresponding label and the drawing area. def on_slider_green_value_changed() @colors[1] = @sliders[1].value.to_i @labels[1].text = "G: " + @sliders[1].value.to_i.to_s update_drawing_color() end # Sets the blue color to the value of the slider, and updates the # corresponding label and the drawing area. def on_slider_blue_value_changed() @colors[2] = @sliders[2].value.to_i @labels[2].text = "B: " + @sliders[2].value.to_i.to_s update_drawing_color() end # Draws a circle on the Gtk::DrawingArea. def on_drawingarea_expose_event() # Get width and height. alloc = @drawing.allocation # Draw a circle. @drawing.window.draw_arc(@drawing.style.fg_gc(@drawing.state), true, 0, 0, alloc.width, alloc.height, 0, 64 * 360) # Draw a rectangle. #@drawing.window.draw_rectangle(@drawing.style.fg_gc(@drawing.state), # true, 0, 0, alloc.width, alloc.height) end end # Create an instance of the class if this file is run by itself. if $0 == __FILE__ prog = MainProgram.new end