Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gradient method for bars
#1
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.


Using a method I found here to create a gradient, I created a method that allows you to create a gradient bar using 1 or more colors.

If using 1 color, it will obviously be a solid color.

But if you use 2 or more, it will automaticly calculate the width and gradient for each section creating a gradient using all the colors you supplied in order.

Code:
class Bitmap
    def grade(x,y,width,height,start,finish)
        for i in 0..width
            r = start.red * (width - i) / width + finish.red * i / width
            g = start.green * (width - i) / width + finish.green * i / width
            b = start.blue * (width - i) / width + finish.blue * i / width
            a = start.alpha * (width - i) / width + finish.alpha * i / width
            fill_rect(x + i,y,1,height,Color.new(r, g, b, a))
        end
    end
    def draw_gradient(x,y,width,height,colors)
        gradient_width = ((width * 1.0) / (colors.size - 1))
           if colors.size == 1
                  grade(x,y,width,height,colors[0],colors[0])
                  return
            end
        for i in 0..(colors.size - 2)
            x_var = (x + (i * gradient_width))
            grade(x_var,y,gradient_width,height,colors[i],colors[i + 1])
        end
    end
end

I created a simple hp bar using the above script as an example -

Code:
class Window_Base < Window
  def draw_hp_bar(actor,x,y,width = 100)
    hp = actor.hp
    max_hp = actor.maxhp
    percentage = ((hp * 1.0) / max_hp)
    bar_width = (percentage * width)
    empty_width = (width - bar_width)
    gray = Color.new(50,50,50,255)
    hp1 = Color.new(149,0,0,255)
    hp2 = Color.new(218,197,7,255)
    hp3 = Color.new(152,255,130,255)
    #draw empty if any
    self.contents.draw_gradient(x + bar_width,y,empty_width - 1,10,[gray])
    #draw gradient
    self.contents.draw_gradient(x,y,bar_width,10,[hp1,hp2,hp3])
    #draw border
    self.contents.fill_rect(x,y,width,1,Color.new(0,0,0,255))
    self.contents.fill_rect(x,y,1,10,Color.new(0,0,0,255))
    self.contents.fill_rect(x + width,y,1,10,Color.new(0,0,0,255))
    self.contents.fill_rect(x,y + 9,width,1,Color.new(0,0,0,255))
  end
end


Get creative!

This is the syntax btw (I'm aware of the terrible grammar in this post)

Code:
self.contents.draw_gradient(x,y,width,height,[color1,color2,color3,color4,etc.])

As for one color, make sure you keep the brackets anyways.
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  SephirothSpawn's Bars Raziel 0 2,107 07-14-2006, 01:00 PM
Last Post: Raziel
  Draw Text Fix Method SephirothSpawn 0 2,110 03-01-2006, 01:00 PM
Last Post: SephirothSpawn
  Hp&sp Bars DerVVulfman 0 2,165 Less than 1 minute ago
Last Post: DerVVulfman



Users browsing this thread: