Class: Excession::CssRegexHueMod

Inherits:
Object
  • Object
show all
Defined in:
lib/excession/css_regex_hue_mod.rb

Constant Summary collapse

HASHSIX =
/#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})(\s|;|})/i
HASHTHREE =
/#([a-f0-9])([a-f0-9])([a-f0-9])(\s|;|})/i
RGBCALL =
/rgb\(\s*(-?[0-9]+)\s*,\s*(-?[0-9]+)\s*,\s*(-?[0-9]+)\)/i
RGBPCTCALL =
/rgb\(\s*(-?[0-9]+)\s*%\s*,\s*(-?[0-9]+)\s*%\s*,\s*(-?[0-9]+)\s*%\s*\)/i

Instance Method Summary collapse

Instance Method Details

#hsl_to_rgb(hsl) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/excession/css_regex_hue_mod.rb', line 54

def hsl_to_rgb(hsl)
  hdeg,s,l = *hsl

  rgb = nil
  if s == 0
    rgb = [l,l,l]
  else
    temp2 =  l < 0.5 ? l*(1+s) : l+s-l*s
    temp1 = 2.0*l-temp2
    h = hdeg/360.0
    temp3 = [h+1.0/3.0, h, h-1.0/3.0].map{|i| i < 0 ? i+1 : (i > 1 ? i-1 : i)}
    rgb = temp3.map do |i|
      if 6.0*i < 1
        temp1 + (temp2-temp1)*6.0*i
      elsif 2*i < 1
        temp2
      elsif 3*i < 2
        temp1 + (temp2-temp1)*((2.0/3.0)-i)*6.0
      else
        temp1
      end
    end
  end
  rgb.map{|i| (i*255).floor}
end

#modify_hsl(hsl, &blk) ⇒ Object



89
90
91
92
93
# File 'lib/excession/css_regex_hue_mod.rb', line 89

def modify_hsl(hsl, &blk)
  newhsl = blk.call(hsl)
  newhsl[0] %= 360
  newhsl
end

#modify_rgb(rgb, &blk) ⇒ Object



95
96
97
# File 'lib/excession/css_regex_hue_mod.rb', line 95

def modify_rgb(rgb, &blk)
  rgb_to_hashsix(hsl_to_rgb(modify_hsl(rgb_to_hsl(rgb), &blk)))
end

#replace_colours(str, &blk) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/excession/css_regex_hue_mod.rb', line 99

def replace_colours(str, &blk)

  str.gsub!(HASHSIX) do
    hexr, hexg, hexb, tend = $1, $2, $3, $4
    modify_rgb([hexr, hexg, hexb].map(&:hex), &blk)+tend
  end

  str.gsub!(HASHTHREE) do 
    hexr, hexg, hexb, tend = $1, $2, $3, $4
    modify_rgb([hexr*2,hexg*2,hexb*2].map(&:hex), &blk)+tend
  end

  str.gsub!(RGBCALL) do
    strr, strg, strb = $1, $2, $3
    modify_rgb([strr, strg, strb].map{|s|Integer(s)}, &blk)
  end
  
  str.gsub!(RGBPCTCALL) do
    strr, strg, strb = $1, $2, $3
    modify_rgb([strr, strg, strb].
               map{|s|Integer(s)}.
               map{|i| (i/100.0) * 255 }, # Don't just multiply by 2.55, it's not accurate
               &blk)
  end
  
  str
end

#rgb_to_hashsix(rgb) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/excession/css_regex_hue_mod.rb', line 80

def rgb_to_hashsix(rgb)
  "#" + rgb.map do |i| 
    str = i.to_s(16)
    raise "Invalid colour (#{rgb.inspect})!" if str.length > 2
    str="0"+str if str.length<2
    str
  end.join
end

#rgb_to_hsl(rgbint) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/excession/css_regex_hue_mod.rb', line 20

def rgb_to_hsl(rgbint)
  rgb = rgbint.map{|i| i/255.0}
  maxcolour = rgb.max
  mincolour = rgb.min

  #puts "maxcolour: #{maxcolour}"
  #puts "mincolour: #{mincolour}"

  cdiff = (maxcolour-mincolour)
  csum = (maxcolour + mincolour)

  #puts "cdiff: #{cdiff}"
  #puts "csum: #{csum}"

  l = csum/2
  #puts "l: #{l}"
  h,s = 0,0
  if maxcolour != mincolour
    s = l < 0.5 ? cdiff/csum : cdiff/(2.0-csum)
    #puts "s: #{s}"
    r,g,b = rgb
    h = if r == maxcolour
          (g-b)/cdiff
        elsif g == maxcolour
          2.0+(b-r)/cdiff
        else
          4.0+(r-g)/cdiff
        end
    #puts "h: #{h}"
  end

  return [(h*360)%360,s,l]
end

#rotate_hue(angle_degrees, str) ⇒ Object

Use this entry point.



11
12
13
14
15
16
# File 'lib/excession/css_regex_hue_mod.rb', line 11

def rotate_hue(angle_degrees, str)
  replace_colours(str) do |hsl| 
    h,s,l = *hsl
    [h+angle_degrees, s, l]
  end
end