Class: OoxmlParser::Color
- Inherits:
-
OOXMLDocumentObject
- Object
- OOXMLDocumentObject
- OoxmlParser::Color
- Includes:
- ColorHelper
- Defined in:
- lib/ooxml_parser/common_parser/common_data/color.rb
Overview
Class for Color in RGB
Constant Summary collapse
- VALUE_FOR_NONE_COLOR =
Value of color if non selected
nil
Constants included from ColorHelper
OoxmlParser::ColorHelper::AUTO_STRING_VALUE
Instance Attribute Summary collapse
-
#alpha_channel ⇒ Integer
Value of alpha-channel.
-
#blue ⇒ Integer
Value of Blue Part.
-
#green ⇒ Integer
Value of Green Part.
-
#properties ⇒ ColorProperties
Properties of color.
-
#red ⇒ Integer
Value of Red Part.
-
#scheme ⇒ String
Color scheme of color.
-
#style ⇒ String
Value of Color Style.
Attributes inherited from OOXMLDocumentObject
Class Method Summary collapse
-
.array_from_const(const_array_name) ⇒ Array, Color
Read array of color from the AllTestData’s constant.
-
.color_indexes ⇒ Array
Deprecated Indexed colors List of color duplicated from ‘OpenXML Sdk IndexedColors` class See msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.indexedcolors.aspx.
-
.generate_random_color ⇒ Color
(also: random)
Random color.
-
.get_rgb_by_color_index(index) ⇒ Color
Color by it’s index.
-
.parse_string(str) ⇒ Color
(also: parse)
Parse color from string.
-
.to_color(something) ⇒ Color
Convert other object type to Color.
Instance Method Summary collapse
-
#==(other) ⇒ True, False
Compare this object to other.
-
#any? ⇒ True, False
Is color not default.
-
#calculate_with_shade!(shade) ⇒ void
Apply shade to color.
-
#calculate_with_tint!(tint) ⇒ void
Apply tint to color.
-
#copy ⇒ Color
Method to copy object.
-
#initialize(new_red = VALUE_FOR_NONE_COLOR, new_green = VALUE_FOR_NONE_COLOR, new_blue = VALUE_FOR_NONE_COLOR, parent: nil) ⇒ Color
constructor
A new instance of Color.
-
#inspect ⇒ String
Inspect of object for debug means.
-
#looks_like?(color_to_check = nil, delta = 8) ⇒ Boolean
To compare color, which look alike.
-
#none? ⇒ True, False
Is color default.
-
#parse_color(color_node) ⇒ Color
Parse color data.
-
#parse_color_model(color_model_parent_node) ⇒ Color
Parse color model data.
-
#parse_scheme_color(scheme_color_node) ⇒ Color
Parse color scheme data.
-
#primary_colors_none? ⇒ Boolean
Check if all three primary colors are none.
-
#to_hex ⇒ String?
(also: #to_int16)
Color in hex value or ‘nil` if color is not defined.
-
#to_s ⇒ String
Result of convert of object to string.
-
#white? ⇒ True, False
Is color white.
-
#within_delta?(other, delta) ⇒ True, False
Check if other color is within delta with current color.
Methods included from ColorHelper
Methods inherited from OOXMLDocumentObject
#boolean_attribute_value, #parse_xml, #with_data?
Methods included from OoxmlObjectAttributeHelper
#attribute_enabled?, #option_enabled?
Methods included from OoxmlDocumentObjectHelper
Constructor Details
#initialize(new_red = VALUE_FOR_NONE_COLOR, new_green = VALUE_FOR_NONE_COLOR, new_blue = VALUE_FOR_NONE_COLOR, parent: nil) ⇒ Color
Returns a new instance of Color.
39 40 41 42 43 44 45 46 47 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 39 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 super(parent: parent) end |
Instance Attribute Details
#alpha_channel ⇒ Integer
Returns Value of alpha-channel.
29 30 31 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 29 def alpha_channel @alpha_channel end |
#blue ⇒ Integer
Returns Value of Blue Part.
20 21 22 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 20 def blue @blue end |
#green ⇒ Integer
Returns Value of Green Part.
18 19 20 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 18 def green @green end |
#properties ⇒ ColorProperties
Returns properties of color.
34 35 36 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 34 def properties @properties end |
#red ⇒ Integer
Returns Value of Red Part.
16 17 18 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 16 def red @red end |
#scheme ⇒ String
Returns color scheme of color.
26 27 28 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 26 def scheme @scheme end |
#style ⇒ String
Returns Value of Color Style.
22 23 24 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 22 def style @style end |
Class Method Details
.array_from_const(const_array_name) ⇒ Array, Color
Read array of color from the AllTestData’s constant
264 265 266 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 264 def array_from_const(const_array_name) const_array_name.map { |current_color| Color.parse_string(current_color) } end |
.color_indexes ⇒ Array
Returns Deprecated Indexed colors List of color duplicated from ‘OpenXML Sdk IndexedColors` class See msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.indexedcolors.aspx.
280 281 282 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 280 def color_indexes @color_indexes ||= File.readlines("#{__dir__}/color/color_indexes.list", chomp: true) end |
.generate_random_color ⇒ Color Also known as: random
Returns random color.
255 256 257 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 255 def generate_random_color Color.new(rand(256), rand(256), rand(256)) end |
.get_rgb_by_color_index(index) ⇒ Color
Returns color by it’s index.
270 271 272 273 274 275 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 270 def get_rgb_by_color_index(index) color_by_index = color_indexes[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_string(str) ⇒ Color Also known as: parse
Parse color from string
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 287 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) ⇒ Color
Convert other object type to Color
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 307 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) ⇒ True, False
Compare this object to other
105 106 107 108 109 110 111 112 113 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 105 def ==(other) if other.is_a?(Color) ((@red == other.red) && (@green == other.green) && (@blue == other.blue)) || (none? && other.white?) || (white? && other.none?) else false end end |
#any? ⇒ True, False
Returns is color not default.
79 80 81 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 79 def any? !none? end |
#calculate_with_shade!(shade) ⇒ void
This method returns an undefined value.
Apply shade to color
156 157 158 159 160 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 156 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) ⇒ void
This method returns an undefined value.
Apply tint to color
147 148 149 150 151 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 147 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 |
#copy ⇒ Color
Method to copy object
98 99 100 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 98 def copy Color.new(@red, @green, @blue) end |
#inspect ⇒ String
Returns inspect of object for debug means.
59 60 61 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 59 def inspect to_s end |
#looks_like?(color_to_check = nil, delta = 8) ⇒ Boolean
To compare color, which look alike
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 128 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_color 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 within_delta?(color_to_check, delta) end |
#none? ⇒ True, False
Returns is color default.
73 74 75 76 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 73 def none? primary_colors_none? || style == :nil end |
#parse_color(color_node) ⇒ Color
Parse color data
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 234 def parse_color(color_node) case color_node.name when 'srgbClr' color = parse_hex_string(color_node.attribute('val').value) color.properties = ColorProperties.new(parent: color).parse(color_node) color when 'schemeClr' color = SchemeColor.new(parent: parent) return ValuedChild.new(:string, parent: parent).parse(color_node) unless root_object.theme scheme_clr_object = ValuedChild.new(:symbol, parent: self).parse(color_node) color.value = root_object.theme.color_scheme[scheme_clr_object.value].color color.properties = ColorProperties.new(parent: color).parse(color_node) color.converted_color = Color.new(parent: self).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) ⇒ Color
Parse color model data
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 201 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 'srgbClr' valued_child = ValuedChild.new(:string, parent: self).parse(color_model_node) color = Color.new.parse_hex_string(valued_child.value) color.alpha_channel = ColorAlphaChannel.new(parent: self).parse(color_model_node).value when 'schemeClr' color = Color.new(parent: self).parse_scheme_color(color_model_node) end end return nil unless color color.calculate_with_tint!(1.0 - tint) if tint @red = color.red @green = color.green @blue = color.blue @alpha_channel = color.alpha_channel @scheme = color.scheme self end |
#parse_scheme_color(scheme_color_node) ⇒ Color
Parse color scheme data
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 165 def parse_scheme_color(scheme_color_node) scheme_clr_object = ValuedChild.new(:symbol, parent: self).parse(scheme_color_node) color_scheme_color = root_object.theme.color_scheme[scheme_clr_object.value] return unless color_scheme_color color = color_scheme_color.color hls = color.to_hsl scheme_name = nil scheme_color_node.xpath('*').each do |scheme_color_node_child| case scheme_color_node_child.name when 'lumMod' luminance_modulation = ValuedChild.new(:float, parent: self).parse(scheme_color_node_child) hls.l = hls.l * (luminance_modulation.value / 100_000.0) when 'lumOff' luminance_offset = ValuedChild.new(:float, parent: self).parse(scheme_color_node_child) hls.l = hls.l + (luminance_offset.value / 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 @red = color.red @green = color.green @blue = color.blue @alpha_channel = ColorAlphaChannel.new(parent: self).parse(scheme_color_node).value @scheme = scheme_name self end |
#primary_colors_none? ⇒ Boolean
Check if all three primary colors are none
90 91 92 93 94 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 90 def primary_colors_none? @red == VALUE_FOR_NONE_COLOR && @green == VALUE_FOR_NONE_COLOR && @blue == VALUE_FOR_NONE_COLOR end |
#to_hex ⇒ String? Also known as: to_int16
Returns color in hex value or ‘nil` if color is not defined.
64 65 66 67 68 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 64 def to_hex return nil if none? (@red.to_s(16).rjust(2, '0') + @green.to_s(16).rjust(2, '0') + @blue.to_s(16).rjust(2, '0')).upcase end |
#to_s ⇒ String
Returns result of convert of object to string.
50 51 52 53 54 55 56 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 50 def to_s if primary_colors_none? 'none' else "RGB (#{@red}, #{@green}, #{@blue})" end end |
#white? ⇒ True, False
Returns is color white.
84 85 86 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 84 def white? (@red == 255) && (@green == 255) && (@blue == 255) end |
#within_delta?(other, delta) ⇒ True, False
Check if other color is within delta with current color
119 120 121 122 123 |
# File 'lib/ooxml_parser/common_parser/common_data/color.rb', line 119 def within_delta?(other, delta) (red - other.red).abs < delta && (green - other.green).abs < delta && (blue - other.blue).abs < delta end |