class Array
#--------------------------------------------------------------------------
# ? count_min
#--------------------------------------------------------------------------
def count_min
hash = {}
for value in self
if hash[value] == nil
hash[value] = 1
else
hash[value] += 1
end
end
array = hash.to_a
array.sort! {| a,b | a[1] <=> b[1]}
return nil if array.empty?
return array[0]
end
#--------------------------------------------------------------------------
# ? count_min!
#--------------------------------------------------------------------------
def count_min!
hash = {}
for value in self
if hash[value] == nil
hash[value] = 1
else
hash[value] += 1
end
end
array = hash.to_a
array.sort! {| a,b | a[1] <=> b[1]}
return nil if array.empty?
delete(array[0][0])
return array[0]
end
#--------------------------------------------------------------------------
# ? count_max
#--------------------------------------------------------------------------
def count_max
hash = {}
for value in self
if hash[value] == nil
hash[value] = 1
else
hash[value] += 1
end
end
array = hash.to_a
array.sort! {| a,b | b[1] <=> a[1]}
return nil if array.empty?
return array[0]
end
#--------------------------------------------------------------------------
# ? count_max!
#--------------------------------------------------------------------------
def count_max!
hash = {}
for value in self
if hash[value] == nil
hash[value] = 1
else
hash[value] += 1
end
end
array = hash.to_a
array.sort! {| a,b | b[1] <=> a[1]}
return nil if array.empty?
delete(array[0][0])
return array[0]
end
end
#==============================================================================
# ? Sprite
#------------------------------------------------------------------------------
# fix z value to 100 at initialize
# allow to set a bitmap at initialize
# allow slick moves by using true_x and true_y values
#==============================================================================
if @true_y != nil
if self.y < @true_y
n = (@true_y - self.y) / 3
n = 1 if n == 0
self.y += n
elsif self.y > @true_y
n = (self.y - @true_y) / 3
n = 1 if n == 0
self.y -= n
end
end
if @true_x != nil
if self.x < @true_x
n = (@true_x - self.x) / 3
n = 1 if n == 0
self.x += n
elsif self.x > @true_x
n = (self.x - @true_x) / 3
n = 1 if n == 0
self.x -= n
end
end
end
end
class Color
def self.hex_to_ary(string)
red = string[0..1].hex
blue = string[2..3].hex
green = string[4..5].hex
return [red, blue, green]
end
#--------------------------------------------------------------------------
# ? aliases
#--------------------------------------------------------------------------
alias initialize_hex initialize
#--------------------------------------------------------------------------
# ? initialize
#--------------------------------------------------------------------------
def initialize(*args)
case args.size
when 1
string = args[0]
red = string[0..1].hex
blue = string[2..3].hex
green = string[4..5].hex
alpha = 255
when 2
string = args[0]
red = string[0..1].hex
blue = string[2..3].hex
green = string[4..5].hex
alpha = args[1]
when 3
red = args[0]
blue = args[1]
green = args[2]
alpha = 255
when 4
red = args[0]
blue = args[1]
green = args[2]
alpha = args[3]
end
initialize_hex(red, blue, green, alpha)
end
#--------------------------------------------------------------------------
# ? negative!
#--------------------------------------------------------------------------
def negative!
return initialize(255 - self.red, 255 - self.green, 255 - self.blue)
end
end
HOW TO USE
#Array
:count_min returns [smallest value, index of the value]
[2, 3, 7, 5, 1, 6] # returns [1, 4]
:count_min!
same as count_min but delete the smallest value
:count_max returns [biggest value, index of the value]
[2, 3, 7, 5, 1, 6] # returns [7, 2]
:count_max!
same as count_max but delete the biggest value
#Sprite
:new(viewport, bitmap)
allows to set the bitmap while initializing the sprite
:true_x(=)
true_x is nil by default. when setting a value for it, the sprite will move smoothly to the
value
:true_y(=)
same as true_x but for y coord.
#String
:wrap(width)
wrap the text according to width's value. return an array containing the different lines.
#Color
Color.hex_to_ary(string)
return an array of decimal numbers.
Color.hex_to_ary("FF6400") # returns [255, 100, 0]
:new(string, opacity)
allows to use HTML hex colors instead of decimal numbers
Color.new("FFFFFF") = Color.new(255, 255, 255)
:negative!
set the color to it's negative.
color = Color.new(0, 0, 0)
color.negative!
color = Color.new(255, 255, 255)
enjoy!
EDIT : added Color negative method
TERMS AND CONDITIONS
As always, Give Credit and Enjoy!