Module: Colour

Included in:
HSV, RGB
Defined in:
lib/colour.rb

Overview

Colour is intended to be a mixin for all representations of colour. It is not intended to be initialized on its own

Instance Method Summary collapse

Instance Method Details

#analogous(degrees = 30, steps = 5, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/colour.rb', line 61

def analogous(degrees=30, steps=5, &block)
    if block_given? then
        rotate_hue(degrees, steps) do |c|
            block.call(c)
        end
    else
        rotate_hue(degrees, steps)
    end
end

#complementaryObject

Return the complementary colour



29
30
31
# File 'lib/colour.rb', line 29

def complementary
    rotate_hue(180)[0]
end

#gradient_to(colour, steps = 10) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/colour.rb', line 82

def gradient_to(colour, steps=10)
    c = self.class.name.downcase
    origin = self.to_rgb
    destination = colour.to_rgb
    gradient = []

    #Special Case, if some joker asks for a gradient of one step
    # the range calculation will result in NaN
    if steps == 1 then
       if block_given? then
         yield self
       end
       return [self] 
    end

    #TODO: *_range isn't really being used
    red_range = destination.r - origin.r
    red_increment = red_range / (steps - 1)
    green_range = destination.g - origin.g
    green_increment = green_range / (steps - 1)
    blue_range = destination.b - origin.b
    blue_increment = blue_range / (steps - 1) 

    steps.times do |i|
        intermediate = RGB.new(
            origin.r + red_increment * i,
            origin.g + green_increment * i,
            origin.b + blue_increment * i
        )
        gradient << intermediate.send("to_" + c)
        if block_given? then
            yield intermediate.send("to_" + c)
        end
    end
    gradient
end

#hexObject



16
17
18
# File 'lib/colour.rb', line 16

def hex
    self.to_rgb.hex
end

#rotate_hue(degrees = 180, steps = 1) ⇒ Object

Move the specified number of degrees, for the specified number of steps



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/colour.rb', line 48

def rotate_hue(degrees=180, steps=1)
    c = self.class.name.downcase
    hsv = self.to_hsv
    v = []
    (1..steps).each do |i|
        v << HSV.new((hsv.h + (degrees * i) + 360 * i) % 360, hsv.s, hsv.v).send("to_" + c)
    end
    if block_given? then
        v.each do |color| yield color end
    end
    v 
end

#split_complementary(spread = 60) ⇒ Object

Return two colours spread distance apart opposite the colour wheel



35
36
37
38
39
40
41
42
43
44
# File 'lib/colour.rb', line 35

def split_complementary(spread=60)
    c = self.class.name.downcase
    hsv = self.to_hsv
    v = [HSV.new(((hsv.h + 540 - spread / 2) % 360),hsv.s,hsv.v).send("to_" + c), 
    HSV.new(((hsv.h + 540 + spread / 2) % 360),hsv.s,hsv.v).send("to_" + c)]
    if block_given? then
        v.each do |color| yield color end
    end
    v  
end

#to_hsvObject



8
9
10
# File 'lib/colour.rb', line 8

def to_hsv
    self
end

#to_rgbObject



4
5
6
# File 'lib/colour.rb', line 4

def to_rgb
    self    
end

#to_sObject



24
25
26
# File 'lib/colour.rb', line 24

def to_s
    self.to_rgb.web_hex
end

#triadic(&block) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/colour.rb', line 71

def triadic(&block)
    if block_given? then
        rotate_hue(120, 2) do |c|
            block.call(c)
        end
    else
        rotate_hue(120,2)
    end
end

#web_hexObject



12
13
14
# File 'lib/colour.rb', line 12

def web_hex
    self.to_rgb.web_hex
end

#web_safe(depth = 1) ⇒ Object



20
21
22
# File 'lib/colour.rb', line 20

def web_safe(depth=1)
    self.to_rgb.web_safe(depth)
end