Created
April 24, 2016 05:37
-
-
Save lrechert/5b12c68ba0ceead3cf4ac9feb1f3e904 to your computer and use it in GitHub Desktop.
Ruby Weekly Challenge - RGB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rgb(r, g, b) | |
"#{to_padded_hex(r)}#{to_padded_hex(g)}#{to_padded_hex(b)}".upcase | |
end | |
def to_padded_hex(n) | |
n = 0 if n < 0 | |
n = 255 if n > 255 | |
n.to_s(16).rjust(2, "0") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../rgb.rb' | |
describe "#rgb" do | |
context "when a negative value is supplied" do | |
it "sets the negative value at 0" do | |
expect(rgb(-40, 255, 255)).to eql "00FFFF" | |
end | |
end | |
context "when a value > 255 is supplied" do | |
it "sets the large value at 255" do | |
expect(rgb(300, 0, 255)).to eql "FF00FF" | |
end | |
end | |
context "when a valid rgb value is supplied" do | |
it "returns hex version of the rgb decimal value" do | |
expect(rgb(0, 0, 0)).to eql "000000" | |
end | |
it "returns hex version of the rgb decimal value" do | |
expect(rgb(255, 255, 255)).to eql "FFFFFF" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment