Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Define a Gradiated Color ~ Tested and Works

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.

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
#------------------------------------------------------------------------------
The call is straightforward -
Code:
self.contents.gradiated_color?(colors,width,percentage)
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.
 
First of all, this is a good script :).

But,
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.red * k / width
         g = colors[i - 1].green * (width - k) / width + colors.green * k / width
         b = colors[i - 1].blue * (width - k) / width + colors.blue * k / width
         a = colors[i - 1].alpha * (width - k) / width + colors.alpha * k / width
        return Color.new(r,g,b,a)
      end
    end
     else
    for k in 0..width
      if k == width_per
         r = colors.red * (width - k) / width + colors[i + 1].red * k / width
         g = colors.green * (width - k) / width + colors[i + 1].green * k / width
         b = colors.blue * (width - k) / width + colors[i + 1].blue * k / width
         a = colors.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

Look in your post. the script was kinda f*cked up with emphasized tekst and [ i ] showing up without reason.

Thirth, I am agianst using methods that make anythingnaming them ?. I got some methods like mouse_over? wich checks something. This is off couyrse not an error or a rule, but I did get somewhat confused when I looked at it. gradiated_color would just be fine right?
 

Kest

Member

Meâ„¢":1wl6n32t said:
Look in your post. the script was kinda f*cked up with emphasized tekst and [ i ] showing up without reason.
Actually, the two are likely related, since the BBCode for italics is [ i ] stuff [/ i ].
 
Thanks, and yes it would be fine. Consdering it creates and checks at the same time. *mumbles incoherently*

Sorry, I just spent an entire night not being able to get to sleep lolz. *feels dizzy*
 
*bump*

I know I'll recieve posts saying to just edit but I find that people are less likely to check a script they think doesn't work.

I apologize for the slight brain fart so-to-speak. The 's that were "messing" up the script were supposed to be there. As I mentioned before I got absolutely no sleep last night and this just passed me without a second thought, until I looked at it and though something was wrong with it, then I checked my other posts and it became apparent.

Once again I ask for your forgiveness and hope you all can find this script useful.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top