Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Background with any rgb value
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Grigoriy Petukhov  
View profile  
 More options Nov 27 2008, 11:59 pm
From: Grigoriy Petukhov <lizen...@gmail.com>
Date: Thu, 27 Nov 2008 20:59:17 -0800 (PST)
Local: Thurs, Nov 27 2008 11:59 pm
Subject: Background with any rgb value
Hi.
I want to fill the background of cell with some RGV value. Is it
possible?
I found only how to fill background with color from predefined set of
colors (xlwt.Style.colour_map_text).

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Nov 28 2008, 3:49 am
From: John Machin <sjmac...@lexicon.net>
Date: Fri, 28 Nov 2008 19:49:02 +1100
Local: Fri, Nov 28 2008 3:49 am
Subject: Re: [pyxl] Background with any rgb value
On 28/11/2008 15:59, Grigoriy Petukhov wrote:

> Hi.
> I want to fill the background of cell with some RGV value. Is it
> possible?
> I found only how to fill background with color from predefined set of
> colors (xlwt.Style.colour_map_text).

Excel 97-2003 doesn't support setting cell background to an arbitrary
RGB value in a generalised fashion. What it does support is the ability
to change the RGB values associated with one or more colour indexes in
the palette. This is not implemented in xlwt (yet).

Here is an OTTOMH sketch of what needs to be done to implement this
facility, in case someone wants to jump in and help:

1. Set up const default palette (grab it from e.g. xlrd.formatting.py).
2. Initialise working palette from default.
3. Provide API Workbook.set_palette_entry(colour_index, red, green,
blue) ... validations: colour_index in range(8, 64); r/g/b in range(256).
4. When saving workbook: if working palette is not the same as the
default palette, write a PALETTE record [there is a stub for this].

Cheers,
John


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sammie  
View profile  
 More options Sep 14 2012, 2:03 pm
From: Sammie <kingjym2...@gmail.com>
Date: Fri, 14 Sep 2012 11:03:33 -0700 (PDT)
Local: Fri, Sep 14 2012 2:03 pm
Subject: Re: Background with any rgb value

I have a RGB tuple (r, g, b) for the background color of a cell, how to I
set the background color using (r,g,b)?
I appreciate your help.

Thanks
Sam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Sep 14 2012, 2:57 pm
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Fri, 14 Sep 2012 12:57:05 -0600
Local: Fri, Sep 14 2012 2:57 pm
Subject: Re: [pyxl] Re: Background with any rgb value

On Fri, Sep 14, 2012 at 12:03 PM, Sammie <kingjym2...@gmail.com> wrote:
> I have a RGB tuple (r, g, b) for the background color of a cell, how to I
> set the background color using (r,g,b)?
> I appreciate your help.

You can't using xlwt.  Prior to Excel 2007, Excel only had color
indexes, and that's all that xlwt supports.  Maybe this will help,
though.  I use a ColorMatcher that takes an RGB input and tries to
return the closest matching Excel color index:

class ColorMatcher(object):

    def __init__(self):
        self.reset()

    def reset(self):
        self.unused_colors = set(self.xlwt_colors)
        # Never use black.
        self.unused_colors.discard((0, 0, 0))

    #Culled from a table at http://www.mvps.org/dmcritchie/excel/colors.htm
    xlwt_colors=[
        (0,0,0), (255,255,255), (255,0,0), (0,255,0), (0,0,255), (255,255,0),
        (255,0,255), (0,255,255), (0,0,0), (255,255,255), (255,0,0), (0,255,0),
        (0,0,255), (255,255,0), (255,0,255), (0,255,255), (128,0,0), (0,128,0),
        (0,0,128), (128,128,0), (128,0,128), (0,128,128), (192,192,192),
        (128,128,128), (153,153,255), (153,51,102), (255,255,204),
        (204,255,255), (102,0,102), (255,128,128), (0,102,204), (204,204,255),
        (0,0,128), (255,0,255), (255,255,0), (0,255,255), (128,0,128),
        (128,0,0), (0,128,128), (0,0,255), (0,204,255), (204,255,255),
        (204,255,204), (255,255,153), (153,204,255), (255,153,204),
        (204,153,255), (255,204,153), (51,102,255), (51,204,204), (153,204,0),
        (255,204,0), (255,153,0), (255,102,0), (102,102,153), (150,150,150),
        (0,51,102), (51,153,102), (0,51,0), (51,51,0), (153,51,0), (153,51,102),
        (51,51,153), (51,51,51)
    ]

    @staticmethod
    def color_distance(rgb1, rgb2):
        # Adapted from Colour metric by Thiadmer Riemersma,
        # http://www.compuphase.com/cmetric.htm
        rmean = (rgb1[0] + rgb2[0]) / 2
        r = rgb1[0] - rgb2[0]
        g = rgb1[1] - rgb2[1]
        b = rgb1[2] - rgb2[2]
        return (((512 + rmean) * r * r) / 256) + 4 * g * g \
            + (((767 - rmean) * b * b) / 256)

    def match_color_index(self, color):
        """Takes an "R,G,B" string or wx.Color and returns a matching xlwt
        color.
        """
        if isinstance(color, int):
            return color
        if color:
            if isinstance(color, basestring):
                rgb = map(int, color.split(','))
            else:
                rgb = color.Get()
            distances = [self.color_distance(rgb, x) for x in self.xlwt_colors]
            result = distances.index(min(distances))
            self.unused_colors.discard(self.xlwt_colors[result])
            return result

    def get_unused_color(self):
        """Returns an xlwt color index that has not been previously returned by
        this instance.  Attempts to maximize the distance between the color and
        all previously used colors.
        """
        if not self.unused_colors:
            # If we somehow run out of colors, reset the color matcher.
            self.reset()
        used_colors = [c for c in self.xlwt_colors if c not in
self.unused_colors]
        result_color = max(self.unused_colors,
                           key=lambda c: min(self.color_distance(c, c2)
                                             for c2 in used_colors))
        result_index = self.xlwt_colors.index(result_color)
        self.unused_colors.discard(result_color)
        return result_index


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam Wong  
View profile  
 More options Sep 14 2012, 3:02 pm
From: Sam Wong <kingjym2...@gmail.com>
Date: Fri, 14 Sep 2012 12:02:08 -0700
Local: Fri, Sep 14 2012 3:02 pm
Subject: Re: [pyxl] Re: Background with any rgb value

Hi Ian:
Thanks very much for your quick reply.
How can I change to use Excel 2007/2010? Do they support setting background
colors using RGB tuple(r, g, b)?

-Sam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam Wong  
View profile  
 More options Sep 14 2012, 3:56 pm
From: Sam Wong <kingjym2...@gmail.com>
Date: Fri, 14 Sep 2012 12:56:52 -0700
Local: Fri, Sep 14 2012 3:56 pm
Subject: Re: [pyxl] Re: Background with any rgb value

Ian:

Thanks again for your quick feedback, your info really helps.

Sammie


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alan Rotman  
View profile  
 More options Sep 15 2012, 1:06 pm
From: Alan Rotman <alan.rot...@gmail.com>
Date: Sat, 15 Sep 2012 10:06:18 -0700 (PDT)
Local: Sat, Sep 15 2012 1:06 pm
Subject: Re: [pyxl] Re: Background with any rgb value

I modified xlwt 6 months ago to add support for setting new colors with an
RGB tuple.
    wbs.set_colour_RGB()
    add_palette_colour()
The code should be in the github trunk. If you can't access it, I could
send you the sources.
I don't know when the next xlwt release will be.

Alan


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
मुग्धा निमकर  
View profile  
 More options Jan 21, 12:16 am
From: मुग्धा निमकर <mnim...@gmail.com>
Date: Sun, 20 Jan 2013 21:16:45 -0800 (PST)
Local: Mon, Jan 21 2013 12:16 am
Subject: Re: [pyxl] Re: Background with any rgb value

Please provide the github trunk. Or If possible can you please send the
sources for set color through RGB in xlwt.

Thanks
Mugdha


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »