Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created April 19, 2012 21:50
Show Gist options
  • Save Jared-Prime/2424438 to your computer and use it in GitHub Desktop.
Save Jared-Prime/2424438 to your computer and use it in GitHub Desktop.
beginning additive color theory
# this gist teaches you how to balance colors using HEX-to-DEC conversions.
# In color theory, additive colors are colors that, when combined, produce solid white.
# HEX primaries, plus black and white
# remember, HEX colors desribe the state of a color channel. think of 00 as "OFF" and FF as "ON".
@red = "FF0000" # red channel fully ON, all else fully OFF
@green = "00FF00" # green channel fully ON, all else fully OFF
@blue = "0000FF" # blue channel fully ON, all else fully OFF
@black = "000000" # all channels fully OFF
@white = "FFFFFF" # all channels fully ON
# in other words, the shade white is produced when all the channels are ON, and the shade black is produced when all the channels are OFF.
# all other colors are produced by mixing the primary channels - red, green, and blue.
# Let's start converting the primary HEX colors to DEC.
class String
def to_dec
@colors = []
self.scan(/.{2}/).each do |component|
@colors << component.to_i(16)
end
return @colors
end
end
puts @red.to_dec
# now we can start doing some math
red_in_dec = @red.to_dec
blue_in_dec = @blue.to_dec
combo = red_in_dec + blue_in_dec
puts combo.to_s
# yet there's a slight problem... we haven't actually added the values in each channel
# try an easy loop
i = 0
new_color = []
while i<3 do
new_color << @red.to_dec[i] + @blue.to_dec[i]
i += 1
end
puts new_color.to_s
# let's make a method out of that!
def add_two_colors(color1,color2)
i = 0
new_color = []
while i<3 do
new_color << @red.to_dec[i] + @blue.to_dec[i]
i += 1
end
return new_color
end
puts add_two_colors(@red,@blue).to_s
# soon, we'll be adding as many colors as we want
# (consider: how much is too much when combining colors?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment