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.

index.js 1.6KB

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