Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. /**
  2. * Given an input of a 6-character hex color code, we need to output a "web-safe"
  3. * representation of that same code.
  4. */
  5.  
  6. 'use strict';
  7.  
  8. let assert = (one, two) => {
  9. if (one === two) {
  10. console.info(`Success! ${one} is equal to ${two}!`);
  11. } else {
  12. console.error(`Failbomb! ${one} is _NOT_ equal to ${two}!`);
  13. }
  14. };
  15.  
  16. const convert = hex_input => {
  17. let valid_input = validate(hex_input);
  18.  
  19. let rgb_values = hex_to_decimal(valid_input);
  20. let rounded_rgb_values = [];
  21. let rounded_hex_rgb_values = [];
  22.  
  23. rgb_values.forEach(i => {
  24. // How to round to multiple of 51?
  25. let safe_value = closest_multiple_of_51(i);
  26. rounded_rgb_values.push(safe_value);
  27. });
  28.  
  29. // convert back to hex
  30. rounded_rgb_values.forEach(i => {
  31. rounded_hex_rgb_values.push(i.toString(16));
  32. });
  33.  
  34. return `#${rounded_hex_rgb_values.join('')}`;
  35. };
  36.  
  37. const validate = string => {
  38. // TODO: normalize valid 3 character color strings to 6 characters; e.g. #333 => #333333
  39. // TODO: actual work here, ensure a 6-character hex string
  40. return string;
  41. };
  42.  
  43. const hex_to_decimal = hex_input => {
  44. // hex_input: #000000
  45. let red_hex = hex_input.substr(1, 2);
  46. let green_hex = hex_input.substr(3, 2);
  47. let blue_hex = hex_input.substr(5, 2);
  48.  
  49. let red_decimal = parseInt(red_hex, 16);
  50. let green_decimal = parseInt(green_hex, 16);
  51. let blue_decimal = parseInt(blue_hex, 16);
  52.  
  53. return [red_decimal, green_decimal, blue_decimal];
  54. };
  55.  
  56. const closest_multiple_of_51 = number => {
  57. let remainder = number % 51;
  58.  
  59. if (remainder === 0) {
  60. return number;
  61. }
  62.  
  63. if (remainder > 51 / 2) {
  64. number += (51 - remainder);
  65. } else {
  66. number -= remainder;
  67. }
  68.  
  69. return number;
  70. };
  71.  
  72. assert(closest_multiple_of_51(0), 0);
  73. assert(closest_multiple_of_51(255), 255);
  74. assert(closest_multiple_of_51(45), 51);
  75. assert(closest_multiple_of_51(65), 51);
  76. assert(closest_multiple_of_51(111), 102);
  77. assert(closest_multiple_of_51(160), 153);
  78. assert(closest_multiple_of_51(90), 102);
  79.  
  80. assert(closest_multiple_of_51(24), 0);
  81. assert(closest_multiple_of_51(25), 0);
  82. assert(closest_multiple_of_51(26), 51);
  83.  
  84. let results = hex_to_decimal('#000033');
  85. assert(results[0], 0);
  86. assert(results[1], 0);
  87. assert(results[2], 51);
  88.  
  89. results = hex_to_decimal('#ae3312');
  90. assert(results[0], 174);
  91. assert(results[1], 51);
  92. assert(results[2], 18);
  93.  
  94. assert(convert('#000044'), '#000033');
  95. assert(convert('#ae3312'), '#993300');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement