Class: AutoColors::ColorScheme

Inherits:
Object
  • Object
show all
Defined in:
lib/autocolors/colorscheme.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ ColorScheme

Returns a new instance of ColorScheme.



6
7
8
9
10
11
12
13
14
15
# File 'lib/autocolors/colorscheme.rb', line 6

def initialize(name=nil)
  srand rand(0xffffffff)
  name ||= Webster.new.random_word
  @name = name
  @seed = @name.hash
  srand @seed
  @dark = {}
  @light = {}
  generate
end

Instance Attribute Details

#contrastObject

Returns the value of attribute contrast.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def contrast
  @contrast
end

#darkObject

Returns the value of attribute dark.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def dark
  @dark
end

#lightObject

Returns the value of attribute light.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def light
  @light
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def name
  @name
end

#saturationObject

Returns the value of attribute saturation.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def saturation
  @saturation
end

#seedObject

Returns the value of attribute seed.



5
6
7
# File 'lib/autocolors/colorscheme.rb', line 5

def seed
  @seed
end

Instance Method Details

#concrete_index(entry, k) ⇒ Object (protected)



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

def concrete_index(entry, k)
  return if entry.data[k].is_a? Integer
  primes = entry.data[k].count("'")
  entry.data[k].gsub! "'",''
  if entry.data[k] =~ /^(\d+)$/ # Direct index
    if primes > 0
      entry.data[k] = new_color(Integer($1), primes, entry.depth)
    else # Otherwise good to go
      entry.data[k] = Integer($1)
    end
  elsif entry.data[k] =~ /^<$/ # Inherit from parent
    if entry.parent.nil?
      $stderr.puts "Mapping refers to parent without having a parent"
      exit(2)
    else
      concrete_index(entry.parent, k)
      if primes == 0 # Directly inherit
        entry.data[k] = entry.parent.data[k]
      else # Modify a bit
        entry.data[k] = new_color(entry.parent.data[k], primes, entry.depth)
      end
    end
  end
end

#concrete_lvl(entry, k) ⇒ Object (protected)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/autocolors/colorscheme.rb', line 79

def concrete_lvl(entry, k)
  return if entry.data[k].is_a? Integer
  c_parent = entry.data[k].count('<')
  c_plus   = entry.data[k].count('+')
  c_minus  = entry.data[k].count('-')
  c_neut   = entry.data[k].count('~')
  val = 0
  if c_parent == 0
    val = 3 - c_minus + c_plus
  else
    concrete_lvl(entry.parent, k)
    offset = entry.parent.data[k]
    val = offset + c_plus - c_minus
  end
  entry.data[k] = [[val,7].min,0].max
end

#concrete_style(entry) ⇒ Object (protected)



96
97
98
# File 'lib/autocolors/colorscheme.rb', line 96

def concrete_style(entry)
  entry.data[:styles] = 'NONE'
end

#dark_i(idx) ⇒ Object (protected)



100
# File 'lib/autocolors/colorscheme.rb', line 100

def dark_i(idx) @intensity[idx] end

#dark_s(idx) ⇒ Object (protected)



102
# File 'lib/autocolors/colorscheme.rb', line 102

def dark_s(idx) @fcolor[idx] end

#do_concrete_mappingObject (protected)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/autocolors/colorscheme.rb', line 32

def do_concrete_mapping
  @mapping = MAPPING.dup
  @mapping.entries.each do |name, entry|
    [:fg_idx, :bg_idx].each{|k| concrete_index(entry,k)}
    [:fg_intensity, :fg_saturation, :bg_intensity, :bg_saturation].each{|k| concrete_lvl(entry,k)}
    concrete_style(entry)
  end

  @mapping.entries.each do |name, entry|
    ldat = entry.data.dup
    ddat = entry.data.dup
    fg_a, fg_b, _ = @base_colors[ldat[:fg_idx]]
    bg_a, bg_b, _ = @base_colors[ldat[:bg_idx]]
    ldat[:fg] = lab(light_i(ldat[:fg_intensity]), light_s(ldat[:fg_saturation]), fg_a, fg_b)
    ldat[:bg] = lab(light_i(ldat[:bg_intensity]), light_s(ldat[:bg_saturation]), bg_a, bg_b)
    ddat[:fg] = lab( dark_i(ldat[:fg_intensity]),  dark_s(ldat[:fg_saturation]), fg_a, fg_b)
    ddat[:bg] = lab( dark_i(ldat[:bg_intensity]),  dark_s(ldat[:bg_saturation]), bg_a, bg_b)
    @dark[name] = ddat
    @light[name] = ldat
  end
end

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/autocolors/colorscheme.rb', line 17

def generate
  bc = nrand(-1.0,1.5,-4.0,5.5)
  sat = rand * 2.0
  @contrast = bc    # Value between -4.0 and 5.0 used to contract/spread out intensity values
  @saturation = sat # Value between 0.0 and 2.0 used to contract/intensify color values

  #@intensity = [0.0-(bc*3.0), 20.0-(bc*2.0), 45.0-bc, 50.0, 60.0, 65.0+bc, 90.0+(bc*2.0), 110.0+(bc*3.0)]
  @intensity = [[3.0 - (bc*3.0),0].max, 20.0-(bc*2.0), 45.0-bc, 50.0, 60.0, 65.0+bc, 90.0+(bc*2.0), 110.0+(bc*3.0)]
  @fcolor = [0.0, 0.1*sat, 0.5*sat, 1.0*sat, 1.5*sat, 2.0*sat, 2.5*sat, 3.0*sat]

  @base_colors = (1..10).map{|i| [nrand(0.0, 70.0, -120.0, 120.0), nrand(0.0,70.0,-120.0,120.0), 1]}
  do_concrete_mapping
end

#lab(intensity, saturation, a, b) ⇒ Object (protected)



105
# File 'lib/autocolors/colorscheme.rb', line 105

def lab(intensity,saturation,a,b) Color.new([intensity, a * saturation, b * saturation]) end

#light_i(idx) ⇒ Object (protected)



101
# File 'lib/autocolors/colorscheme.rb', line 101

def light_i(idx) @intensity[7 - idx] - 0.005  end

#light_s(idx) ⇒ Object (protected)



103
# File 'lib/autocolors/colorscheme.rb', line 103

def light_s(idx) @fcolor[idx] + 0.01 end

#new_color(base_idx, diff_level, depth) ⇒ Object (protected)



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/autocolors/colorscheme.rb', line 107

def new_color(base_idx, diff_level, depth)
  a,b,count = @base_colors[base_idx]
  base_diff = (diff_level.to_f + 1.0) * 4.0 / ((depth.to_f + 1.0) / 2.0) * count.to_f
  a_dir = rand(2) == 1 ? -1.0 : 1.0
  b_dir = rand(2) == 1 ? -1.0 : 1.0
  a_p = a + (base_diff * a_dir)
  b_p = b + (base_diff * b_dir)
  new_idx = @base_colors.size
  @base_colors[new_idx] = [a_p, b_p, 1]
  @base_colors[base_idx][2] += 1
  return new_idx
end

#nrand(mean = 0, stddev = nil, floor = nil, ceil = nil) ⇒ Object (protected)



123
124
125
126
127
128
129
130
131
132
# File 'lib/autocolors/colorscheme.rb', line 123

def nrand(mean=0, stddev=nil, floor=nil, ceil=nil)
  theta = 2 * Math::PI * rand
  rho = Math.sqrt(-2 * Math.log(1 - rand))
  scale = stddev * rho
  res = (rand >= 0.5) ? Math.cos(theta) : Math.sin(theta)
  res = mean.to_f + scale * res
  res = floor if (! floor.nil?) && (res < floor)
  res = ceil  if (! ceil.nil?) && (res > ceil)
  return res
end

#nrand_colorObject (protected)



121
# File 'lib/autocolors/colorscheme.rb', line 121

def nrand_color; nrand(0.0, 40.0, -120.0, 120.0) end

#rand_colorObject (protected)



120
# File 'lib/autocolors/colorscheme.rb', line 120

def rand_color; rand(180) - 90 end