03-03-2008, 05:09 AM
Drawing a Gradient Bar
by RPG Advocate
saved from Phylomortis.Com
by RPG Advocate
saved from Phylomortis.Com
Introduction
This script is a skeleton script that can be used in any window. It draws a bar of the specified length with a gradient starting at the color specified by gradient_red_start, gradient_green_start, and gradient_blue_start and ending at the colors specified by gradient_red_end, gradient_green_end, and gradient_blue_end. As shown here, the bar will be all black, so replace the 0s with the appropriate values. If you want the whole bar to be drawn 100% of the time, simply replace [your equation here] with 100. Otherwise, replace this with the equation you want to determine what percentage of the bar is drawn. Note that if draw_bar_percent is 0 and the length is at least 100, one pixel of the bar will still be drawn, so you may want to include a special case in your code that sets draw_bar_percent to -1 if you don't want any of the bar to be drawn under some circumstances. Replace bar_height with how tall you want the bar to be, in pixels. Note that the bars in the image are superimposed over a black bar not included in this script.
Script
def draw_gradient_bar(x, y, length)
gradient_red_start = 0
gradient_red_end = 0
gradient_green_start = 0
gradient_green_end = 0
gradient_blue_start = 0
gradient_blue_end = 0
draw_bar_percent = [your equation here]
for x_coord in 1..length
current_percent_done = x_coord * 100 / length
difference = gradient_red_end - gradient_red_start
red = gradient_red_start + difference * x_coord / length
difference = gradient_green_end - gradient_green_start
green = gradient_green_start + difference * x_coord / length
difference = gradient_blue_end - gradient_blue_start
blue = gradient_blue_start + difference * x_coord / length
if current_percent_done <= draw_bar_percent
rect = Rect.new(x + x_coord-1, y, 1, bar_height)
self.contents.fill_rect(rect, Color.new(red, green, blue, 255))
end
end
end