Guest User

Untitled

a guest
Jan 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. /**
  2. * Calculate Bus Line Color
  3. * @author Trewbot
  4. * @param lineName - The name of the bus line as a string.
  5. * @return A 6 digit hexadecimal string describing line color.
  6. */
  7. function lineColor(lineName){
  8. if(typeof lineName !== "string") throw new Error("Parameter 'abbr' must be a string");
  9. function letterValue(letter){
  10. if(letter.length !== 1) throw new Error("Parameter 'letter' must be a single character");
  11. const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  12. var value = alphabet.indexOf(letter.toUpperCase());
  13. if(value < 0) throw new Error("Parameter 'letter' must be alphanumeric");
  14. return value;
  15. }
  16. function HSVtoRGB(h,s,v){
  17. if(typeof h !== "number" || typeof s !== "number" || typeof v !== "number")
  18. throw new Error("Parameters 'h', 's', and 'v' must be numbers");
  19. if(h < 0 || h > 1 || s < 0 || s > 1 || v < 0 || v > 1)
  20. throw new Error("Parameters must be numbers between 0 and 1");
  21. var r,g,b,
  22. i=Math.floor(h*6),
  23. f=h*6-i,
  24. p=v*(1-s),
  25. q=v*(1-f*s),
  26. t=v*(1-(1-f)*s);
  27. switch(i%6){
  28. case 0:r=v,g=t,b=p;break;
  29. case 1:r=q,g=v,b=p;break;
  30. case 2:r=p,g=v,b=t;break;
  31. case 3:r=p,g=q,b=v;break;
  32. case 4:r=t,g=p,b=v;break;
  33. case 5:r=v,g=p,b=q;break;
  34. }
  35. return[r*255,g*255,b*255];
  36. }
  37. function toHexadecimal(n){
  38. if(typeof n !== "number") throw new Error("Paramter 'n' must be a number");
  39. n = parseInt(n,10); // Force n to be an integer anyway
  40. if(isNaN(n)) return "00"; // If that didn't work just return zeroes
  41. if(n < 0 || n > 255) throw new Error("Parameter 'n' must be in range 0 <= n < 256");
  42. n = Math.max(0,Math.min(n,255)); // Force n into range anyway
  43. const hex = "0123456789ABCDEF";
  44. return hex.charAt((n-n%16)/16)+hex.charAt(n%16);
  45. }
  46. // Calculate a hue using first to letters of lineName
  47. var h = letterValue(lineName.charAt(0))/26
  48. + letterValue(lineName.charAt(1))/(26**2),
  49. rgb = HSVtoRGB(h,1,1);
  50. return toHexadecimal(rgb[0])+toHexadecimal(rgb[1])+toHexadecimal(rgb[2]);
  51. }
Add Comment
Please, Sign In to add comment