# 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
class OoxmlParser::Color
Class for Color
in RGB
Constants
- VALUE_FOR_NONE_COLOR
Value of color if non selected
Attributes
@return [Integer] Value of alpha-channel
@return [Integer] Value of Blue Part
@return [Integer] Value of Green Part
@return [ColorProperties] properties of color
@return [Integer] Value of Red Part
@return [String] color scheme of color
@return [Integer] Value of alpha-channel
@return [String] Value of Color
Style
@return [String] Value of Color
Style
Public Class Methods
Read array of color from the AllTestData’s constant @param [Array] const_array_name - array of the string @return [Array, Color] array of color
# 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
@return [Array] Deprecated Indexed colors
List of color duplicated from `OpenXML Sdk IndexedColors` class See https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.indexedcolors.aspx
# 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
@return [Color] random color
# 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
@param index [Integer] index to get @return [Color] color by it’s index
# 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
# 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
Parse color from string @param str [String] string to parse @return [Color] result of parsing
# 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
Convert other object type to Color
@param something [Object] object to convert @return [Color] result of conversion
# 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
Public Instance Methods
Compare this object to other @param other [Object] any other object @return [True, False] result of comparison
# 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
@return [True, False] is color not default
# File lib/ooxml_parser/common_parser/common_data/color.rb, line 79 def any? !none? end
Apply shade to color @param shade [Integer] shade to apply @return [void]
# 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
Apply tint to color @param tint [Integer] tint to apply @return [void]
# 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
Method to copy object @return [Color] copied object
# File lib/ooxml_parser/common_parser/common_data/color.rb, line 98 def copy Color.new(@red, @green, @blue) end
@return [String] inspect of object for debug means
# File lib/ooxml_parser/common_parser/common_data/color.rb, line 59 def inspect to_s end
To compare color, which look alike @param [String or Color] color_to_check color to compare @param [Integer] delta max delta for each of specters
# 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
@return [True, False] is color default
# File lib/ooxml_parser/common_parser/common_data/color.rb, line 73 def none? primary_colors_none? || style == :nil end
Parse color data @param color_node [Nokogiri::XML:Element] node to parse @return [Color] result of parsing
# 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 data @param color_model_parent_node [Nokogiri::XML:Element] node to parse @return [Color] result of parsing
# 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 color scheme data @param scheme_color_node [Nokogiri::XML:Element] node to parse @return [Color] result of parsing
# 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
Check if all three primary colors are none @return [Boolean]
@return [String, nil] color in hex value or ‘nil` if color is not defined
# 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
@return [String] result of convert of object to string
# 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
@return [True, False] is color white
# File lib/ooxml_parser/common_parser/common_data/color.rb, line 84 def white? (@red == 255) && (@green == 255) && (@blue == 255) end
Check if other color is within delta with current color @param other [Color] color to compare @param delta [Integer] max delta for each of specters @return [True, False] result of comparison
# 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