# Gdk::Color to HTML Format Hex

**URL:** <https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595>\
**Category:** Ruby-Gnome 2\
**Created:** [August 11, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595 "2009-08-11T20:48:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595/1 "2009-08-11T20:48:47Z")

</div>

I’m trying to convert the hex code that Gdk::Color.to\_s() returns into  
HTML Format Hex. I think what needs to be done is some math on the  
string, but I’m fairly new to ruby and not sure how to do this. For  
instance, if the color in HTML hex is a six string code, #ffffff, then  
Gdk::Color would return #ffffffffffff.

Here is an example of how to do it in C, but I’m not sure how to  
translate that to ruby.

> **[Gdk.Color -\> HTML hex string](http://volatile-minds.blogspot.com/2009/04/gdkcolor-html-hex-string.html)**
>
> String.Format("#{0:X2}{1:X2}{2:X2}", (byte)(colorSelector.CurrentColor.Red \>\> 8), (byte)(colorSelector.CurrentColor.Green \>\> 8), (byte)(colo...

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595/2 "2009-08-11T21:44:52Z")

</div>

str = (color.red \>\> 8).to\_s(16)  
str += (color.green \>\> 8).to\_s(16)  
str += (color.blue \>\> 8).to\_s(16)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595/3 "2009-08-11T22:15:19Z")

</div>

Scrap that: “#%02x%02x%02x” % [c.red, c.green, c.blue]

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tue, 11 Aug 2009 19:55:39 +0100  
Subject: Re: [ruby-gnome2-devel-en] Gdk::Color to HTML Format Hex

str = (color.red \>\> 8).to\_s(16)  
str += (color.green \>\> 8).to\_s(16)  
str += (color.blue \>\> 8).to\_s(16)

> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day  
> trial. Simplify your report design, integration and deployment - and focus on  
> what you do best, core application coding. Discover what’s new with  
> Crystal Reports now. [http://p.sf.net/sfu/bobj-july](http://p.sf.net/sfu/bobj-july)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

Internet Explorer 8 - accelerate your Hotmail. Download Internet  
Explorer 8

* * *

Upgrade to Internet Explorer 8 Optimised for MSN.

[http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U](http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [August 11, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/gdk-color-to-html-format-hex/173595/4 "2009-08-11T22:15:54Z")

</div>

Damnit, I’m having a real idiot day today …

Scrap that: “#%02x%02x%02x” % [c.red, c.green, c.blue].map {|i| i \>\> 8}

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tue, 11 Aug 2009 19:58:33 +0100  
Subject: Re: [ruby-gnome2-devel-en] Gdk::Color to HTML Format Hex

Scrap that: “#%02x%02x%02x” % [c.red, c.green, c.blue]

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tue, 11 Aug 2009 19:55:39 +0100  
Subject: Re: [ruby-gnome2-devel-en] Gdk::Color to HTML Format Hex

str = (color.red \>\> 8).to\_s(16)  
str += (color.green \>\> 8).to\_s(16)  
str += (color.blue \>\> 8).to\_s(16)

> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day  
> trial. Simplify your report design, integration and deployment - and focus on  
> what you do best, core application coding. Discover what’s new with  
> Crystal Reports now. [http://p.sf.net/sfu/bobj-july](http://p.sf.net/sfu/bobj-july)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

Internet Explorer 8 - accelerate your Hotmail. Download Internet  
Explorer 8  
Celebrate a decade of Messenger with free winks, emoticons, display  
pics, and more. Get Them Now

* * *

Upgrade to Internet Explorer 8 Optimised for MSN.

[http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U](http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U)
