Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rgb2hex.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;(function(window) {
  2. var rgb2hex = function(color) {
  3. if(typeof color !== 'string') {
  4. // throw error of input isn't typeof string
  5. throw new Error('color has to be type of `string`');
  6. } else if (color.substr(0, 1) === '#') {
  7. // or return if already rgb color
  8. return {
  9. hex: color,
  10. alpha: 1
  11. };
  12. }
  13. /**
  14. * strip spaces
  15. */
  16. var strippedColor = color.replace(/\s+/g,'');
  17. /**
  18. * parse input
  19. */
  20. var digits = /(.*?)rgb(a)??\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([01]|1.0*|0??\.([0-9]{0,})))??\)/.exec(strippedColor);
  21. if(!digits) {
  22. // or throw error if input isn't a valid rgb(a) color
  23. throw new Error('given color (' + color + ') isn\'t a valid rgb or rgba color');
  24. }
  25. var red = parseInt(digits[3], 10);
  26. var green = parseInt(digits[4], 10);
  27. var blue = parseInt(digits[5], 10);
  28. var alpha = digits[6] ? /([0-9\.]+)/.exec(digits[6])[0] : '1';
  29. var rgb = ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1);
  30. // parse alpha value into float
  31. if(alpha.substr(0,1) === '.') {
  32. alpha = parseFloat('0' + alpha);
  33. }
  34. // limit alpha value to 1
  35. if(alpha > 1) {
  36. alpha = 1;
  37. }
  38. // cut alpha value after 2 digits after comma
  39. alpha = parseFloat(Math.round(alpha * 100)) / 100;
  40. return {
  41. hex: '#' + rgb.toString(16),
  42. alpha: alpha
  43. };
  44. };
  45. if(typeof define === 'function' && define.amd) {
  46. define('rgb2hex', function () {
  47. return rgb2hex;
  48. });
  49. } else {
  50. window.rgb2hex = rgb2hex;
  51. }
  52. }(window));