Class: Dcr::Command::Color

Inherits:
Dcr::Command show all
Defined in:
app/models/dcr/command/color.rb

Constant Summary collapse

COLOR_MAP =

color map from strings to Glimmer DSL for SWT colors

{
  'black' => :black,
  'blue' => :cyan,
  'gray' => :gray,
  'grey' => :gray,
  'green' => :green,
  'orange' => [255, 127, 0],
  'pink' => :magenta,
  'purple' => :dark_magenta,
  'red' => :red,
  'white' => :white,
  'yellow' => :yellow,
}
COLOR_MAP_ALIASES =
{
  'grey' => :gray,
  'k' => :black,
  'b' => :cyan,
  'a' => :gray,
  'g' => :green,
  'o' => [255, 127, 0],
  'i' => :magenta,
  'p' => :dark_magenta,
  'r' => :red,
  'w' => :white,
  'y' => :yellow,
}

Class Attribute Summary collapse

Attributes inherited from Dcr::Command

#text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dcr::Command

command_alias, command_aliases, command_exclusion, command_exclusions, command_operation, create, #initialize, match?, #normalized_text, #operation, #operation=, operation_derivatives, #operation_options, parse_operation, parse_value, #parsed_operation, #parsed_value, #value=

Constructor Details

This class inherits a constructor from Dcr::Command

Class Attribute Details

.next_color_indexObject (readonly)

Returns the value of attribute next_color_index.



56
57
58
# File 'app/models/dcr/command/color.rb', line 56

def next_color_index
  @next_color_index
end

Class Method Details

.color_derivatives(color_string) ⇒ Object



69
70
71
72
# File 'app/models/dcr/command/color.rb', line 69

def color_derivatives(color_string)
  color_string_length = color_string.length
  color_string_length.times.map {|n| color_string.chars.combination(color_string_length - n).to_a}.reduce(:+).map(&:join)
end

.expanded_color_mapObject



58
59
60
61
62
63
64
65
66
67
# File 'app/models/dcr/command/color.rb', line 58

def expanded_color_map
  @expanded_color_map ||= COLOR_MAP.reduce({}) do |hash, pair|
    color_string = pair.first
    color_value = pair.last
    color_hash = color_derivatives(color_string).reduce({}) do |color_derivative_hash, color_derivative|
      color_derivative_hash.merge(color_derivative => color_value)
    end
    hash.merge(color_hash)
  end.merge(COLOR_MAP_ALIASES)
end

.next_colorObject



80
81
82
# File 'app/models/dcr/command/color.rb', line 80

def next_color
  COLOR_MAP[next_color_string]
end

.next_color_stringObject



74
75
76
77
78
# File 'app/models/dcr/command/color.rb', line 74

def next_color_string
  reset_next_color_index! if @next_color_index.nil?
  @next_color_index += 1
  COLOR_MAP.keys[@next_color_index % COLOR_MAP.count]
end

.normalize_color_string(color_string) ⇒ Object



88
89
90
91
# File 'app/models/dcr/command/color.rb', line 88

def normalize_color_string(color_string)
  color_value = Color.expanded_color_map[color_string]
  COLOR_MAP.invert[color_value]
end

.reset_next_color_index!(next_color_index_value = nil) ⇒ Object



84
85
86
# File 'app/models/dcr/command/color.rb', line 84

def reset_next_color_index!(next_color_index_value=nil)
  @next_color_index = next_color_index_value || -2 # start at yellow (since it skips one with the first addition)
end

Instance Method Details

#callObject



94
95
96
97
# File 'app/models/dcr/command/color.rb', line 94

def call
  @program.polygons.last&.background = interpreted_value
  @program.new_polygon!
end

#interpreted_valueObject

interpreted value is used by GUI



110
111
112
113
# File 'app/models/dcr/command/color.rb', line 110

def interpreted_value
  the_value = value
  COLOR_MAP[the_value]
end

#next_color_stringObject



115
116
117
# File 'app/models/dcr/command/color.rb', line 115

def next_color_string
  Color.next_color_string
end

#valueObject



99
100
101
102
103
104
105
106
107
# File 'app/models/dcr/command/color.rb', line 99

def value
  value_string = super.to_s
  if @value_string != value_string || @value_string.empty?
    @value_string = value_string
    # TODO beware of the edge case where if the last value string is empty, the comparable, which includes value, ends up returning a match with yellow (first color in series) thus not updating GUI
    @value = Color.expanded_color_map.keys.include?(value_string) ? Color.normalize_color_string(value_string) : next_color_string
  end
  @value
end