Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculate Bus Line Color
- * @author Trewbot
- * @param lineName - The name of the bus line as a string.
- * @return A 6 digit hexadecimal string describing line color.
- */
- function lineColor(lineName){
- if(typeof lineName !== "string") throw new Error("Parameter 'abbr' must be a string");
- function letterValue(letter){
- if(letter.length !== 1) throw new Error("Parameter 'letter' must be a single character");
- const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var value = alphabet.indexOf(letter.toUpperCase());
- if(value < 0) throw new Error("Parameter 'letter' must be alphanumeric");
- return value;
- }
- function HSVtoRGB(h,s,v){
- if(typeof h !== "number" || typeof s !== "number" || typeof v !== "number")
- throw new Error("Parameters 'h', 's', and 'v' must be numbers");
- if(h < 0 || h > 1 || s < 0 || s > 1 || v < 0 || v > 1)
- throw new Error("Parameters must be numbers between 0 and 1");
- var r,g,b,
- i=Math.floor(h*6),
- f=h*6-i,
- p=v*(1-s),
- q=v*(1-f*s),
- t=v*(1-(1-f)*s);
- switch(i%6){
- case 0:r=v,g=t,b=p;break;
- case 1:r=q,g=v,b=p;break;
- case 2:r=p,g=v,b=t;break;
- case 3:r=p,g=q,b=v;break;
- case 4:r=t,g=p,b=v;break;
- case 5:r=v,g=p,b=q;break;
- }
- return[r*255,g*255,b*255];
- }
- function toHexadecimal(n){
- if(typeof n !== "number") throw new Error("Paramter 'n' must be a number");
- n = parseInt(n,10); // Force n to be an integer anyway
- if(isNaN(n)) return "00"; // If that didn't work just return zeroes
- if(n < 0 || n > 255) throw new Error("Parameter 'n' must be in range 0 <= n < 256");
- n = Math.max(0,Math.min(n,255)); // Force n into range anyway
- const hex = "0123456789ABCDEF";
- return hex.charAt((n-n%16)/16)+hex.charAt(n%16);
- }
- // Calculate a hue using first to letters of lineName
- var h = letterValue(lineName.charAt(0))/26
- + letterValue(lineName.charAt(1))/(26**2),
- rgb = HSVtoRGB(h,1,1);
- return toHexadecimal(rgb[0])+toHexadecimal(rgb[1])+toHexadecimal(rgb[2]);
- }
Add Comment
Please, Sign In to add comment