title: gtk2hs - converting Color to a hex triplet
body: <p>I'm working in Haskell with gtk2hs, and have come across a problem I can't find an answer to. </p>
  
  <p>I am writing a very simple program: it takes a colour value (or multiple colour values), and then will apply a function to them.  It can do things like complementary colours, the mean of a set of colours, and so on. I've finished putting the interface together in Glade, and have read a few simple tutorials on using gtk2hs; everything works so far, and my interface will show up when I run my program.</p>
  
  <p>However, my problem is with the <code>ColorSelection</code> widget. When I get my value from it, the value it gives is of the type <code>Color Word16 Word16 Word16</code>, where each <code>Word16</code> is between 0 and 65535. What I want to be able to do is to convert that into a Hex triplet (as a <code>String</code>) ideally, or something that I can convert into a Hex triplet. So far, I've found out that it seems to be something to do with the fact that colours are stored as rrrrggggbbbb, rather than the HTML-style rrggbb. What I've found, using this code:</p>
  
  <pre><code>colorToHex (Color a b c)
      =  (showHex a "") ++ " " ++ (showHex b "") ++ " " ++ (showHex c "")
  </code></pre>
  
  <p>is that, although most of the time it's accurate, when using the colour picker tool I can input a colour that seems different to what is output. For example, using the colour picker, I selected <code>#A9D06E</code> - however, my function returned <code>"aa11 d12d 6e41"</code>. Although fairly close, I can't work out the relationship - how does the rounding for the red channel work in this example? Also, if I type the hex value <code>#A9D06E</code> directly, I get <code>"a9a9 d0d0 6e6e"</code>.</p>
  
  <p>I have tried using Hoogle to find a function which converted a <code>Colour</code> to a <code>String</code>, or a <code>Colour</code> to anything else, and have searched the documents on Hackage for the Gtk package, but did not find anything there that would do what I wanted. I have also searched the internet, and could not find much about dealing with the <code>Colour</code> type. I did find one function for doing exactly what I wanted, located at the bottom <a href="http://hdiff.luite.com/cgit/apelsin/tree/src/Preferences.hs?id=4f8dec8a2b146e575edce8836bc286bc6794caf6" rel="nofollow">here</a>.  However, this had the same problem, in that it wouldn't give the same value as the colour picker itself gave for a particular selection.</p>
  
  <h3>Update 1</h3>
  
  <p>I have also tried bitshifting the values. Shifting by 8 bits almost always gives the right value, but, as per the other things I tried, every so often it is slightly out.</p>