Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 25817

Show HN: Colors Used by Popular Sites

$
0
0

In order to organize this data for the above charts, I had to convert all the colors used into a single format. Here I'll explain the different color formats and how I converted them all to HSL.

Predefined Color Names

Browsers recognize certain predefined color names. 140 names are supported by all browsers. The names range from common words like white and red to stranger examples like LightGoldenRodYellow, PapayaWhip, IndianRed and AliceBlue.

RGB (Red, Green, Blue)

Digital colors are made by combining different amounts of red, green and blue light. The amount of each color is represented by a number between 0 and 255.

This is how to write the RGB color format: rgb(186, 218, 85). The first number is red, the second is green and the third is blue.

RGBA (Red, Green, Blue, Alpha)

There's another version of RGB which is frequently used on the web. RGBA adds an additional parameter; alpha. Alpha determines the transparency or opacity of the color. 0 is completely transparent. 1 is completely opaque.

rgba(186, 218, 85, 0) vs rgba(186, 218, 85, 1)

Hexadecimal

The most common color format on the web is hexadecimal. Hexadecimal colors are another way to represent RGB colors.

Hexadecimal numbers are base 16 instead of base 10, so each character represents a number between 0 and 15 instead of 0 and 9.

Here's how to convert base 16 to base 10 in javascript.
  1. base16ToBase10('f0');

  2. function base16ToBase10(base16){

  3. return parseInt(base16,16);

  4. }

Here's how to do the inverse: convert base 10 to base 16.
  1. base10ToBase16(186);

  2. function base10ToBase16(base10){

  3. var base16 = parseFloat(base10).toString(16);

  4. // If the hex number is 1 character long, add a 0 to the front.

  5. if(base16.length==1){

  6. base16 ='0'+ base16;

  7. }

  8. return base16;

  9. }

A hexadecimal color is six characters long and preceded by a number sign: #BADA55. Hexadecimal colors can be split into 4 sections: # + BA + DA + 55. We can ignore the first section: #. This section tells the browser that a hexadecimal color is coming.

The remaining 3 sections contain important information about the color. Hexadecimal colors are based off of the RGB (Red, Green, Blue) color model and each section defines how much of one of those colors is present in the final color.

Red: BA Green: DA Blue: 55

Here are the color values for #BADA55, converted into base 10.

Red: 186 Green: 218 Blue: 85
Here's how to convert a hexadecimal color to RGB.
  1. hexToRgb('#BADA55');

  2. function hexToRgb(color){

  3. var red = base16ToBase10( color.substring(1,3));

  4. var green = base16ToBase10( color.substring(3,5));

  5. var blue = base16ToBase10( color.substring(5,7));

  6. return'rgb('+ red +','+ green +','+ blue +')';

  7. }

Here's how to convert an RGB color to hexadecimal.
  1. rgbToHex('rgb(100,222,0)');

  2. function rgbToHex(color){

  3. var temp_color = color.replace('rgb(','');

  4. temp_color = temp_color.replace(')','');

  5. temp_color = temp_color.split(',');

  6. var red = base10ToBase16(temp_color[0]);

  7. var green = base10ToBase16(temp_color[1]);

  8. var blue = base10ToBase16(temp_color[2]);

  9. return'#'+ red + green + blue;

  10. }

3 Digit Hexadecimal

Sometimes you'll see 3 digit hexadecimal colors like this: #000. This means each color pair had 2 identical digits.

f = ff so #fff = #ffffff and #3a9 = #33aa99.
Here's how to convert a 3 digit hexadecimal color to 6 digits in javascript:
  1. threeDigitsToSix('#000');

  2. function threeDigitsToSix(color){

  3. hex = color.split('');

  4. return'#'+ hex[1]+ hex[1]+ hex[2]+ hex[2]+ hex[3]+ hex[3];

  5. }

HSL (Hue, Saturation, Lightness)

HSL is a color format that attempts to match how humans view color by organizing by hue, saturation and lightness.

Hue

Hue is the most common way for people to describe colors. Hue refers to the shade of a color. Red, Green, Blue, Pink and Orange are all examples of hue.

In the hsl color model hue is plotted around a circle, so is represented as a number between 0 and 360.

Saturation

Saturation is the purity of a color, or how much grey is in the color.

A low saturation color is almost completely grey, black or white. A high saturation color is almost completely its hue. Saturation is represented as a percentage between 0 and 100.

Lightness

Lightness determines whether a color is dark or light. A 100% is white and a 0% is black.

HSLA (Hue, Saturation, Lightness, Alpha)

Similar to RGBA, HSLA has an alpha channel to determine transparency.

Converting from RGB to HSL

The first step is to convert all red, green and blue values into decimals between 0 and 1.

Then you determine the “min” and “max.” The min is the smallest decimal and the max is the largest decimal.

To discover the lightness, add the min and max values together and then divide by 2.

Once we have these values we can determine whether there is saturation and hue. If min and max are the same, then the saturation is 0. If the saturation is 0, then the hue is 0.

If min and max aren't the same, then we need to determine the saturation. Depending on the lightness there are 2 different formulas to use.

If the lightness is below 0.5 then the saturation equals (max-min)/(max+min)

If the lightness is larger than 0.5 then the saturation equals (max-min)/(2-max-min)

Now that we know the lightness and saturation, we can determine the hue. The formula to determine hue depends on which color was the "max."

If red was the max then hue equals (green-blue)/(max-min)

If green was the max then hue equals 2+(blue-red)/(max-min)

If blue was the max then hue equals 4+(red-green)/(max-min)

After making this calculation you need to convert hue to a value between and 255. Multiply the value by 42.6. If it is below 0, then add 255 to the value.

Here's how to convert an RGB color to HSL in javascript:
  1. rgbToHsl('rgb(100,50,0)');

  2. function rgbToHsl(rgb){

  3. var channels = rgb.replace('rgb(','');

  4. channels = channels.replace(')','');

  5. channels = channels.split(',');

  6. var red = channels[0]/=255;

  7. var green = channels[1]/=255;

  8. var blue = channels[2]/=255;

  9. /* Getting the Max and Min values. */

  10. var max =Math.max.apply(Math,[red,green,blue]);

  11. var min =Math.min.apply(Math,[red,green,blue]);

  12. var lightness =(min + max)/2;

  13. if(min === max){

  14. var saturation =0;

  15. var hue =0;

  16. }else{

  17. if( lightness <0.5){

  18. var saturation =(max-min)/(max+min);

  19. }else{

  20. var saturation =(max-min)/(2-max-min);

  21. }

  22. if(red == max){

  23. var temp_hue =(green-blue)/(max-min);

  24. }elseif(green == max){

  25. var temp_hue =2+(blue-red)/(max-min);

  26. }elseif(blue == max){

  27. var temp_hue =4+(red-green)/(max-min);

  28. }

  29. var hue = temp_hue *42.6;

  30. if(hue <0){

  31. hue +=255;

  32. }

  33. }

  34. saturation *=100;

  35. lightness *=100;

  36. return'hsl('+ hue +','+ saturation +','+ lightness +')';

  37. }

We're Done!

The HSL color model is very close to how people interpret colors, so it is the most useful model for organization and analysis. Now that all of our colors are in HSL we can organize by hue, saturation and lightness.


Viewing all articles
Browse latest Browse all 25817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>