Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to convert RGB decimal to RGB hex and vise versa in Excel sheets?

by thanos1983 (Parson)
on Apr 20, 2017 at 18:00 UTC ( [id://1188458]=perlquestion: print w/replies, xml ) Need Help??

thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

My question is related to Getting the background color of a cell in an existing Excel spreadsheet. I was working on resolving the question and although that it looks that the question is resolved I got stuck.

My problem is that when I define the color as decimal int coming from parsed Excel sheet from module Spreadsheet::ParseExcel works just fine, sample of code:

my %opts = (bg_color => $front_color, align => "center", pattern => $pattern, ); # Add and define a format my $format_2 = $workbook->add_format(%opts);

If I use the methods:

my $sRGB = $parser->ColorIdxToRGB($format->{Fill}->[2]); my $sRGB_2 = $workbook->color_idx_to_rgb($format->{Fill}->[2]);

I can convert the int to RGB format (e.g. ffff00), but when I try to convert it back to int using this method set_custom_color($index, $red, $green, $blue):

my $backgroundcolor = $workbook->set_custom_color($back_color, $sRGB);

I also tried to apply straight RGB hex directly ĺike:

my %opts = (bg_color => "ffff00", align => "center", pattern => $pattern, ); # Add and define a format my $format_2 = $workbook->add_format(%opts);

But the output is not as source Excel sheet in all of my attempts, unless if I use directly parsed int.

Sample of code that replicates my problem, and printout of expect output.

#!/usr/bin/perl use strict; use warnings; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook_parse = $parser->Parse( 'Report.xls' ); my $worksheet_parse = $workbook_parse->Worksheet("Sheet1"); my ( $col_min, $col_max ) = $worksheet_parse->col_range(); my ( $row_min, $row_max ) = $worksheet_parse->row_range(); for my $col ( $col_min .. $col_max ) { for my $row ( $row_min .. $row_max ) { # Return the cell object at $row and $col my $cell = $worksheet_parse->get_cell( $row, $col ); next unless $cell; my $value = $cell->value(); my $format = $cell->get_format(); # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new( 'perl.xls' ); # Add a worksheet my $worksheet = $workbook->add_worksheet( "Health_Report" ); my $pattern = $format->{Fill}->[0]; my $front_color = $format->{Fill}->[1]; my $back_color = $format->{Fill}->[2]; my $sRGB = $parser->ColorIdxToRGB($format->{Fill}->[2]); my $backgroundcolor = $workbook->set_custom_color($back_color, $sRGB); use Color::Rgb; # Linux rgb file (I do not know if same directory in WindowsOS) my $rgb = new Color::Rgb(rgb_txt=>'/etc/X11/rgb.txt'); my $yellow_hex = $rgb->hex('yellow'); print "RGB Yellow: " . $yellow_hex . "\n"; print "RGB Excel: " . $sRGB . "\n"; print "RGB int: " . $backgroundcolor . "\n"; my %opts = (bg_color => $front_color, align => "center", pattern => $pattern, ); # Add and define a format my $format_2 = $workbook->add_format(%opts); # Write a formatted and unformatted string, row and column notatio +n. $worksheet->write($row, $col, $value, $format_2); } } __END__ $ perl excel.pl RGB Yellow: ffff00 RGB Excel: FFFF00 RGB int: 34

The reason that I am trying to convert RGB int to hex RGB and back to RGB in is because I was told that different Excel sheets can have different output, this is why we need to apply hex RGB.

Anyone has encounter this problem before?

Thanks in advance for your time and effort.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to convert RGB decimal to RGB hex and vise versa in Excel sheets?
by davies (Prior) on Apr 20, 2017 at 19:48 UTC

    You are using .xls. I don't know the details of the file format, but that was for Excel 2003 & earlier. These versions of Excel had an internal colour palette of 56 colours. Any colour was assigned to the member of the palette that was closest. So you may be seeing some assignment to an assumed palette. This is only a guess as I do my manipulation of Excel either directly in VBA or via Win32::OLE.

    Regards,

    John Davies

      Hello davies,

      Thanks for the reply, but it sounds a bit strange that only accepts RGB int and the RGB hex does not work.

      Well I guess there I will continue experimentation just in case that I will come up with a solution.

      Thanks again for your time.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to convert RGB decimal to RGB hex and vise versa in Excel sheets?
by stevieb (Canon) on Apr 21, 2017 at 13:50 UTC

    Whether this is your issue or not I don't know, as I'm not set up to test it. After a quick glance at some documentation, set_custom_color() takes the following arguments:

    $workbook->set_custom_color(40, 255, 102, 0 ); # Orange $workbook->set_custom_color(40, 0xFF, 0x66, 0x00); # Same thing $workbook->set_custom_color(40, '#FF6600' ); # Same thing

    So it looks like you either need to break up the $sRGB variable into separate hex values, or simply quote the whole thing and prepend it with a #:

    $workbook->set_custom_color($back_color, "#$sRGB");

      Hello stevieb,

      I have test this scenario already, but unfortunately it does not work as I expect it returns another colour.

      Thank you for your time and effort reading and replying to my question.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1188458]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (9)
As of 2024-04-23 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found