Converting an RGB color tuple to a hexidecimal string
Use the format operator %: >>> ‘#%02x%02x%02x’ % (0, 128, 64) ‘#008040′ Note that it won’t check bounds… >>> ‘#%02x%02x%02x’ % (0, -1, 9999) ‘#00-1270f’
Use the format operator %: >>> ‘#%02x%02x%02x’ % (0, 128, 64) ‘#008040′ Note that it won’t check bounds… >>> ‘#%02x%02x%02x’ % (0, -1, 9999) ‘#00-1270f’
The specific numbers in the question are from CCIR 601 (see Wikipedia article). If you convert RGB -> grayscale with slightly different numbers / different methods, you won’t see much difference at all on a normal computer screen under normal lighting conditions — try it. Here are some more links on color in general: Wikipedia … Read more
Among several options for shading and tinting: For shades, multiply each component by 1/4, 1/2, 3/4, etc., of its previous value. The smaller the factor, the darker the shade. For tints, calculate (255 – previous value), multiply that by 1/4, 1/2, 3/4, etc. (the greater the factor, the lighter the tint), and add that to … Read more
I’m failing to see the problem here. The code looks good to me. The only thing I can think of is that the try/catch blocks are redundant — Color is a struct and R, G, and B are bytes, so c can’t be null and c.R.ToString(), c.G.ToString(), and c.B.ToString() can’t actually fail (the only way … Read more
It’s probably best to use the Python Image Library to do this which I’m afraid is a separate download. The easiest way to do what you want is via the load() method on the Image object which returns a pixel access object which you can manipulate like an array: from PIL import Image im = … Read more
TLDR Use this clean one-line function with both rgb and rgba support: const rgba2hex = (rgba) => `#${rgba.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/).slice(1).map((n, i) => (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n)).toString(16).padStart(2, ‘0’).replace(‘NaN’, ”)).join(”)}` 2021 updated answer Much time has passed since I originally answered this question. Then cool ECMAScript 5 and 2015+ features become largely available on … Read more
See Wikipedia’s article on Color Difference for the right leads. Basically, you want to compute a distance metric in some multidimensional colorspace. But RGB is not “perceptually uniform”, so your Euclidean RGB distance metric suggested by Vadim will not match the human-perceived distance between colors. For a start, L*a*b* is intended to be a perceptually … Read more
Libpng-1.6 is more stringent about checking ICC profiles than previous versions. You can ignore the warning. To get rid of it, remove the iCCP chunk from the PNG image. Some applications treat warnings as errors; if you are using such an application you do have to remove the chunk. You can do that with any … Read more
Garry Tan posted a Javascript solution on his blog (which he attributes to a now defunct mjijackson.com, but is archived here and the original author has a gist – thanks to user2441511). The code is re-posted below: HSL to RGB: /** * Converts an HSL color value to RGB. Conversion formula * adapted from http://en.wikipedia.org/wiki/HSL_color_space. … Read more
The method could vary depending on your needs. Here are 3 ways to calculate Luminance: Luminance (standard for certain colour spaces): (0.2126*R + 0.7152*G + 0.0722*B) source Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B) source Luminance (perceived option 2, slower to calculate): sqrt( 0.241*R^2 + 0.691*G^2 + 0.068*B^2 ) → sqrt( 0.299*R^2 + … Read more