rubyXL
rubyXL copied to clipboard
System color ignored in get_rgb (and fix provided)
When there is no rgb
color and a theme is used instead, Cell.get_rgb
retrieves the color from a_srgb_clr
:
def get_rgb(workbook)
if rgb then
return rgb
elsif theme then
theme_color = workbook.theme.get_theme_color(theme)
rgb_color = theme_color && theme_color.a_srgb_clr
This does not work when a system color is used (theme = 0). a_sys_clr
should then be used instead of a_srgb_clr
.
Working fix:
def get_rgb(workbook)
if rgb then
return rgb
elsif theme then
theme_color = workbook.theme.get_theme_color(theme)
rgb_color = theme_color && theme_color.a_srgb_clr
color_value = rgb_color && rgb_color.val
unless color_value then
rgb_color = theme_color && theme_color.a_sys_clr
color_value = rgb_color && rgb_color.last_clr
end
return nil if color_value.nil?
RubyXL::RgbColor.parse(color_value).to_hls.apply_tint(tint).to_rgb.to_s
end
end
Here's a real example:
> cell
=> #<RubyXL::Cell(51,1): "68", datatype="s", style_index=50>
> color = cell.get_cell_font.color
=> #<RubyXL::Color:0x0000557d3e904bb8 @local_namespaces=[], @auto=nil, @indexed=nil, @rgb=nil, @theme=0, @tint=-0.1499984740745262>
> color.get_rgb(cell.worksheet.workbook)
=> nil
> theme = color.theme
=> 0
> theme_color = cell.worksheet.workbook.theme.get_theme_color(theme)
=> #<RubyXL::CT_Color:0x0000557d3e498bd8 @local_namespaces=[], @a_scrgb_clr=nil, @a_srgb_clr=nil, @a_hsl_clr=nil, @a_sys_clr=#<RubyXL::CT_SystemColor:0x0000557d3e498750 @local_namespaces=[], @val="window", @last_clr="FFFFFF", @a_tint=nil, @a_shade=nil, @a_comp=nil, @a_inv=nil, @a_gray=nil, @a_alpha=nil, @a_alpha_off=nil, @a_alpha_mod=nil, @a_hue=nil, @a_hue_off=nil, @a_hue_mod=nil, @a_sat=nil, @a_sat_off=nil, @a_sat_mod=nil, @a_lum=nil, @a_lum_off=nil, @a_lum_mod=nil, @a_red=nil, @a_red_off=nil, @a_red_mod=nil, @a_green=nil, @a_green_off=nil, @a_green_mod=nil, @a_blue=nil, @a_blue_off=nil, @a_blue_mod=nil, @a_gamma=nil, @a_inv_gamma=nil>, @a_scheme_clr=nil, @a_prst_clr=nil>
> rgb_color = theme_color && theme_color.a_srgb_clr
=> nil
> rgb_color = theme_color && theme_color.a_sys_clr
=> #<RubyXL::CT_SystemColor:0x0000557d3e498750 @local_namespaces=[], @val="window", @last_clr="FFFFFF", @a_tint=nil, @a_shade=nil, @a_comp=nil, @a_inv=nil, @a_gray=nil, @a_alpha=nil, @a_alpha_off=nil, @a_alpha_mod=nil, @a_hue=nil, @a_hue_off=nil, @a_hue_mod=nil, @a_sat=nil, @a_sat_off=nil, @a_sat_mod=nil, @a_lum=nil, @a_lum_off=nil, @a_lum_mod=nil, @a_red=nil, @a_red_off=nil, @a_red_mod=nil, @a_green=nil, @a_green_off=nil, @a_green_mod=nil, @a_blue=nil, @a_blue_off=nil, @a_blue_mod=nil, @a_gamma=nil, @a_inv_gamma=nil>
> color_value = rgb_color && rgb_color.last_clr
=> "FFFFFF"