Tsunokiette
Member
Hello, this is a method which allows you to input a gradient and return a color on a point of the gradient depending on what data you give it. This allows for creating bars that gradualy change color as they deplete.
It supports any number of colors which will be used in the order you supply them. (100% will return the last color)
First off is the code, place this above every other script.
The call is straightforward -
Colors = The colors to be used to create a faux gradient
Width = The width the bar is going to be, is used to determine where to find the color
Percentage = The percentage of how full the bar is, is also used to determine where to find the color
EDIT: replaced code with fixed code supplied by Me TM.
It supports any number of colors which will be used in the order you supply them. (100% will return the last color)
First off is the code, place this above every other script.
Code:
#------------------------------------------------------------------------------
# Begin Bitmap Edit
#------------------------------------------------------------------------------
class Bitmap
def gradiated_color?(colors,width,percentage)
width_per = ((width * 1.0) * percentage)
width_per = width_per.round
if colors.size == 1
return colors[0]
elsif colors.size > 1
i = 0
sections_per = (1.0 / colors.size)
equationa = (sections_per * colors.size)
equationb = (sections_per * (colors.size - 1))
if equationa >= percentage and equationb < percentage
i = (colors.size - 1)
else
for j in 0..(colors.size - 1)
equationc = (sections_per * (j + 1))
equationd = (sections_per * (j + 2))
if equationc < percentage and equationd > percentage
i = j + 1
end
end
end
if i == (colors.size - 1)
for k in 0..width
if k == width_per
r = colors[i - 1].red * (width - k) / width + colors[i].red * k / width
g = colors[i - 1].green * (width - k) / width + colors[i].green * k / width
b = colors[i - 1].blue * (width - k) / width + colors[i].blue * k / width
a = colors[i - 1].alpha * (width - k) / width + colors[i].alpha * k / width
return Color.new(r,g,b,a)
end
end
else
for k in 0..width
if k == width_per
r = colors[i].red * (width - k) / width + colors[i + 1].red * k / width
g = colors[i].green * (width - k) / width + colors[i + 1].green * k / width
b = colors[i].blue * (width - k) / width + colors[i + 1].blue * k / width
a = colors[i].alpha * (width - k) / width + colors[i + 1].alpha * k / width
return Color.new(r,g,b,a)
end
end
end
elsif colors.size < 1
p 'ERROR : No Colors Were Supplied'
exit
end
end
end
#------------------------------------------------------------------------------
# End Bitmap Edit
#------------------------------------------------------------------------------
Code:
self.contents.gradiated_color?(colors,width,percentage)
Width = The width the bar is going to be, is used to determine where to find the color
Percentage = The percentage of how full the bar is, is also used to determine where to find the color
EDIT: replaced code with fixed code supplied by Me TM.