Class: OoxmlParser::Color

Inherits:
OOXMLDocumentObject show all
Includes:
ColorHelper
Defined in:
lib/ooxml_parser/common_parser/common_data/color.rb

Constant Summary collapse

COLOR_INDEXED =

List of color duplicated from ‘OpenXML Sdk IndexedColors` class See msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.indexedcolors.aspx

%w[
  000000
  FFFFFF
  FF0000
  00FF00
  0000FF
  FFFF00
  FF00FF
  00FFFF
  000000
  FFFFFF
  FF0000
  00FF00
  0000FF
  FFFF00
  FF00FF
  00FFFF
  800000
  008000
  000080
  808000
  800080
  008080
  C0C0C0
  808080
  9999FF
  993366
  FFFFCC
  CCFFFF
  660066
  FF8080
  0066CC
  CCCCFF
  000080
  FF00FF
  FFFF00
  00FFFF
  800080
  800000
  008080
  0000FF
  00CCFF
  CCFFFF
  CCFFCC
  FFFF99
  99CCFF
  FF99CC
  CC99FF
  FFCC99
  3366FF
  33CCCC
  99CC00
  FFCC00
  FF9900
  FF6600
  666699
  969696
  003366
  339966
  003300
  333300
  993300
  993366
  333399
  333333
  n/a
  n/a
].freeze
VALUE_FOR_NONE_COLOR =

Value of color if non selected

nil

Instance Attribute Summary collapse

Attributes inherited from OOXMLDocumentObject

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColorHelper

#parse_hex_string

Methods inherited from OOXMLDocumentObject

add_to_xmls_stack, copy_file_and_rename_to_zip, current_xml, dir, encrypted_file?, get_link_from_rels, unzip_file, #with_data?

Methods included from OoxmlDocumentObjectHelper

#to_hash

Constructor Details

#initialize(new_red = VALUE_FOR_NONE_COLOR, new_green = VALUE_FOR_NONE_COLOR, new_blue = VALUE_FOR_NONE_COLOR, parent: nil) ⇒ Color



108
109
110
111
112
113
114
115
116
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 108

def initialize(new_red = VALUE_FOR_NONE_COLOR,
               new_green = VALUE_FOR_NONE_COLOR,
               new_blue = VALUE_FOR_NONE_COLOR,
               parent: nil)
  @red = new_red
  @green = new_green
  @blue = new_blue
  @parent = parent
end

Instance Attribute Details

#alpha_channelInteger



99
100
101
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 99

def alpha_channel
  @alpha_channel
end

#blueInteger



91
92
93
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 91

def blue
  @blue
end

#greenInteger



89
90
91
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 89

def green
  @green
end

#positionObject

Returns the value of attribute position.



102
103
104
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 102

def position
  @position
end

#propertiesObject

Returns the value of attribute properties.



103
104
105
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 103

def properties
  @properties
end

#redInteger



87
88
89
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 87

def red
  @red
end

#schemeString



96
97
98
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 96

def scheme
  @scheme
end

#styleString



93
94
95
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 93

def style
  @style
end

Class Method Details

.array_from_const(const_array_name) ⇒ Array, Color

Read array of color from the AllTestData’s constant



212
213
214
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 212

def array_from_const(const_array_name)
  const_array_name.map { |current_color| Color.parse_string(current_color) }
end

.generate_random_colorObject Also known as: random



203
204
205
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 203

def generate_random_color
  Color.new(rand(256), rand(256), rand(256))
end

.get_rgb_by_color_index(index) ⇒ Object



216
217
218
219
220
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 216

def get_rgb_by_color_index(index)
  color_by_index = COLOR_INDEXED[index]
  return :unknown if color_by_index.nil?
  color_by_index == 'n/a' ? Color.new : Color.new.parse_hex_string(color_by_index)
end

.parse_color(color_node) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 313

def parse_color(color_node)
  case color_node.name
  when 'srgbClr'
    color = Color.new.parse_hex_string(color_node.attribute('val').value)
    color.properties = ColorProperties.new(parent: color).parse(color_node)
    color
  when 'schemeClr'
    color = SchemeColor.new
    color.value = ThemeColors.list[color_node.attribute('val').value.to_sym]
    color.properties = ColorProperties.new(parent: color).parse(color_node)
    color.converted_color = parse_scheme_color(color_node)
    color.value.calculate_with_tint!(1.0 - color.properties.tint) if color.properties.tint
    color
  end
end

.parse_color_model(color_model_parent_node) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 287

def parse_color_model(color_model_parent_node)
  color = nil
  tint = nil
  color_model_parent_node.xpath('*').each do |color_model_node|
    color_model_node.xpath('*').each do |color_mode_node_child|
      case color_mode_node_child.name
      when 'tint'
        tint = color_mode_node_child.attribute('val').value.to_f / 100_000.0
      end
    end
    case color_model_node.name
    when 'scrgbClr'
      color = Color.new(color_model_node.attribute('r').value.to_i, color_model_node.attribute('g').value.to_i, color_model_node.attribute('b').value.to_i)
      color.alpha_channel = ColorAlphaChannel.parse(color_model_node)
    when 'srgbClr'
      color = Color.new.parse_hex_string(color_model_node.attribute('val').value)
      color.alpha_channel = ColorAlphaChannel.parse(color_model_node)
    when 'schemeClr'
      color = parse_scheme_color(color_model_node)
    end
  end
  return nil unless color
  color.calculate_with_tint!(1.0 - tint) if tint
  color
end

.parse_scheme_color(scheme_color_node) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 263

def parse_scheme_color(scheme_color_node)
  color = ThemeColors.list[scheme_color_node.attribute('val').value.to_sym]
  hls = HSLColor.rgb_to_hsl(color)
  scheme_name = nil
  scheme_color_node.xpath('*').each do |scheme_color_node_child|
    case scheme_color_node_child.name
    when 'lumMod'
      hls.l = hls.l * (scheme_color_node_child.attribute('val').value.to_f / 100_000.0)
    when 'lumOff'
      hls.l = hls.l + (scheme_color_node_child.attribute('val').value.to_f / 100_000.0)
    end
  end
  scheme_color_node.attributes.each do |key, value|
    case key
    when 'val'
      scheme_name = value.to_s
    end
  end
  color = hls.to_rgb
  color.alpha_channel = ColorAlphaChannel.parse(scheme_color_node)
  color.scheme = scheme_name
  color
end

.parse_string(str) ⇒ Object Also known as: parse



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 222

def parse_string(str)
  return str if str.is_a?(Color)
  return Color.new(VALUE_FOR_NONE_COLOR, VALUE_FOR_NONE_COLOR, VALUE_FOR_NONE_COLOR) if str == 'none' || str == '' || str == 'transparent' || str.nil?
  split = if str.include?('RGB (') || str.include?('rgb(')
            str.gsub(/[(RGBrgb\(\) )]/, '').split(',')
          elsif str.include?('RGB ') || str.include?('rgb')
            str.gsub(/RGB |rgb/, '').split(', ')
          else
            raise "Incorrect data for color to parse: '#{str}'"
          end

  Color.new(split[0].to_i, split[1].to_i, split[2].to_i)
end

.to_color(something) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 238

def to_color(something)
  case something
  when SchemeColor
    something.converted_color
  when DocxColorScheme
    something.color
  when Fill
    something.to_color
  when PresentationFill
    if something.color.respond_to? :converted_color
      something.color.converted_color
    else
      something.color
    end
  when String
    Color.parse(something)
  when Symbol
    Color.parse(something.to_s)
  when DocxColor
    Color.parse(something.value)
  else
    something
  end
end

Instance Method Details

#==(other) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 153

def ==(other)
  if other.is_a?(Color)
    if nil?
      false
    elsif (@red == other.red) && (@green == other.green) && (@blue == other.blue)
      true
    elsif (none? && other.white?) || (white? && other.none?)
      true
    else
      false
    end
  else
    false
  end
end

#any?Boolean



141
142
143
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 141

def any?
  !none?
end

#calculate_with_shade!(shade) ⇒ Object



196
197
198
199
200
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 196

def calculate_with_shade!(shade)
  @red = (@red * shade.to_f).to_i
  @green = (@green * shade.to_f).to_i
  @blue = (@blue * shade.to_f).to_i
end

#calculate_with_tint!(tint) ⇒ Object



190
191
192
193
194
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 190

def calculate_with_tint!(tint)
  @red += (tint.to_f * (255 - @red)).to_i
  @green += (tint.to_f * (255 - @green)).to_i
  @blue += (tint.to_f * (255 - @blue)).to_i
end

#copyObject



149
150
151
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 149

def copy
  Color.new(@red, @green, @blue)
end

#inspectObject



126
127
128
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 126

def inspect
  to_s
end

#looks_like?(color_to_check = nil, delta = 8) ⇒ Boolean

To compare color, which look alike



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 172

def looks_like?(color_to_check = nil, delta = 8)
  color_to_check = color_to_check.converted_color if color_to_check.is_a?(SchemeColor)
  color_to_check = color_to_check.pattern_fill.foreground_folor if color_to_check.is_a?(Fill)
  color_to_check = color_to_check.color.converted_color if color_to_check.is_a?(PresentationFill)
  color_to_check = Color.parse(color_to_check) if color_to_check.is_a?(String)
  color_to_check = Color.parse(color_to_check.to_s) if color_to_check.is_a?(Symbol)
  color_to_check = Color.parse(color_to_check.value) if color_to_check.is_a?(DocxColor)
  return true if none? && color_to_check.nil?
  return true if none? && color_to_check.none?
  return false if none? && color_to_check.any?
  return false if !none? && color_to_check.none?
  return true if self == color_to_check
  red = color_to_check.red
  green = color_to_check.green
  blue = color_to_check.blue
  (self.red - red).abs < delta && (self.green - green).abs < delta && (self.blue - blue).abs < delta ? true : false
end

#none?Boolean



136
137
138
139
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 136

def none?
  (@red == VALUE_FOR_NONE_COLOR) && (@green == VALUE_FOR_NONE_COLOR) && (@blue == VALUE_FOR_NONE_COLOR) ||
    (style == :nil)
end

#to_hexObject Also known as: to_int16



130
131
132
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 130

def to_hex
  (@red.to_s(16).rjust(2, '0') + @green.to_s(16).rjust(2, '0') + @blue.to_s(16).rjust(2, '0')).upcase
end

#to_sObject



118
119
120
121
122
123
124
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 118

def to_s
  if @red == VALUE_FOR_NONE_COLOR && @green == VALUE_FOR_NONE_COLOR && @blue == VALUE_FOR_NONE_COLOR
    'none'
  else
    "RGB (#{@red}, #{@green}, #{@blue})"
  end
end

#white?Boolean



145
146
147
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 145

def white?
  (@red == 255) && (@green == 255) && (@blue == 255)
end