{"id":4639,"date":"2014-03-30T14:14:24","date_gmt":"2014-03-30T14:14:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/storing-lab-colors-as-integers-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:14:24","modified_gmt":"2014-03-30T14:14:24","slug":"storing-lab-colors-as-integers-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/storing-lab-colors-as-integers-collection-of-common-programming-errors\/","title":{"rendered":"Storing LAB colors as integers-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/cf86dc9634b5cf5a627b1c9dc5cbf6fe?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nXSL<\/p>\n<p>When using RGB values, .NET has built in ways for it to be converted from an integer and it&#8217;s straightforward to convert a color to an int. Is there a way of storing LAB colors as integers instead? Unlike RGB, LAB color values can be negative. I don&#8217;t want to have to store the colors in RGB and convert them at runtime if I can avoid it as I have no need for RGB values.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/acc6c90045526b55e7bc5fe0322f3ba7?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmmgp<\/p>\n<p>So the transformation being done is RGB -&gt; XYZ with the old 2 degree observer and CIE Standard Illuminant D65 -&gt; CIELAB. The code, for reference, for performing that is given below (R, G, B is assumed to be in [0, 1]).<\/p>\n<p>Considering these transformations starting from 8 bits per channel in RGB, the range for L* is [0, 100], a* (-85.92, 97.96], b* (-107.54, 94.20]. These values are close approximations. In theory, a* and b* are unbounded, but you will find some places that talk about a limit of +-128, +-110, etc. My suggestion is then to simply sum 128 to each value, multiply it by 100, and then round to integer (that should be precise enough for a color). Any given L*a*b triplet can then be represented by a 16 bits unsigned integer. You could pack them into a 64 bit integer. And after unpacking you would subtract 128 from each value. If you can keep three signed short integers, things get much simpler.<\/p>\n<pre><code>def rgb_xyz(r, g, b): # 2o. D65\n    triple = [r, g, b]\n\n    v, d = 0.04045, 12.94\n    for i, c in enumerate(triple):\n        triple[i] = 100 * (c \/ d if c  t0 else a * c + b\n\n    l = 116 * triple[0] - 16\n    a = 500 * (triple[0] - triple[1])\n    b = 200 * (triple[1] - triple[2])\n    return l, a, b\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>XSL When using RGB values, .NET has built in ways for it to be converted from an integer and it&#8217;s straightforward to convert a color to an int. Is there a way of storing LAB colors as integers instead? Unlike RGB, LAB color values can be negative. I don&#8217;t want to have to store the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4639","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4639"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4639\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}