Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function areColorsSimilar(hexColor1, hexColor2)
- {
- // convert from hex to rgb array
- var col1 = new Array();
- var col2 = new Array();
- col1['r'] = HexToR(hexColor1);
- col1['g'] = HexToG(hexColor1);
- col1['b'] = HexToB(hexColor1);
- col2['r'] = HexToR(hexColor2);
- col2['g'] = HexToG(hexColor2);
- col2['b'] = HexToB(hexColor2);
- brightness1 = parseInt((parseInt(col1['r']*299,10) + parseInt(col1['g']*587,10) + parseInt(col1['b']*114,10))/1000,10);
- brightness2 = parseInt((parseInt(col2['r']*299,10) + parseInt(col2['g']*587,10) + parseInt(col2['b']*114,10))/1000,10);
- brightnessRange = Math.max(brightness1,brightness2) - Math.min(brightness1,brightness2);
- // get color difference
- diffRed = Math.max(col1['r'],col2['r']) - Math.min(col1['r'],col2['r']);
- diffGreen = Math.max(col1['g'],col2['g']) - Math.min(col1['g'],col2['g']);
- diffBlue = Math.max(col1['b'],col2['b']) - Math.min(col1['b'],col2['b']);
- colorDifference = parseInt(diffRed + diffGreen + diffBlue,10);
- if(brightnessRange <= 125 && colorDifference <= 500)
- return true;
- return false;
- }
- function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
- function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
- function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
- function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
Advertisement
Add Comment
Please, Sign In to add comment