Created
October 22, 2014 02:15
-
-
Save fisherds/74de463007a49ffdfa15 to your computer and use it in GitHub Desktop.
Code to handle a lot of the details of color conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** namespace. */ | |
| var rh = rh || {}; | |
| rh.color = rh.color || {}; | |
| rh.color.rgb_to_hex_mode = true; | |
| rh.color.redValue = 181; | |
| rh.color.greenValue = 9; | |
| rh.color.blueValue = 57; | |
| rh.color.hexString = "#b50939"; | |
| /* | |
| * Helper methods to go from hex to RGB | |
| * from http://www.javascripter.net/faq/hextorgb.htm | |
| */ | |
| rh.color.removeHexChar = function(h) { | |
| return (h.charAt(0)=="#") ? h.substring(1,7):h; | |
| }; | |
| rh.color.hexToR = function(h) { | |
| return parseInt((rh.color.removeHexChar(h)).substring(0,2), 16); | |
| }; | |
| rh.color.hexToG = function(h) { | |
| return parseInt((rh.color.removeHexChar(h)).substring(2,4), 16); | |
| }; | |
| rh.color.hexToB = function(h) { | |
| return parseInt((rh.color.removeHexChar(h)).substring(4,6), 16); | |
| }; | |
| /* | |
| * Helper methods to go from RGB to hex | |
| * from http://www.javascripter.net/faq/rgbtohex.htm | |
| */ | |
| rh.color.rgbToHex = function(R, G, B) { | |
| return "#" + rh.color.toHex(R) + rh.color.toHex(G) + rh.color.toHex(B); | |
| }; | |
| rh.color.toHex = function(n) { | |
| n = parseInt(n, 10); | |
| if (isNaN(n)) | |
| return "00"; | |
| n = Math.max(0, Math.min(n, 255)); | |
| return "0123456789ABCDEF".charAt((n - n % 16) / 16) | |
| + "0123456789ABCDEF".charAt(n % 16); | |
| }; | |
| rh.color.updateView = function() { | |
| // TODO: Implement | |
| }; | |
| rh.color.convertColorUsingRgbInputs = function() { | |
| // Input: RGB inputs | |
| rh.color.redValue = Math.max(0, Math.min($("#input-red").val(), 255)); | |
| rh.color.greenValue = Math.max(0, Math.min($("#input-green").val(), 255)); | |
| rh.color.blueValue = Math.max(0, Math.min($("#input-blue").val(), 255)); | |
| // Output: Hex value | |
| rh.color.hexString = rh.color.rgbToHex(rh.color.redValue, rh.color.greenValue, rh.color.blueValue); | |
| rh.color.updateView(); | |
| }; | |
| rh.color.convertColorUsingHexInput = function() { | |
| // Input: Hex value input | |
| rh.color.hexString = $("#input-hex").val(); | |
| // Output: RGB | |
| rh.color.redValue = rh.color.hexToR(rh.color.hexString); | |
| rh.color.greenValue = rh.color.hexToG(rh.color.hexString); | |
| rh.color.blueValue = rh.color.hexToB(rh.color.hexString); | |
| rh.color.updateView(); | |
| }; | |
| rh.color.enableButtons = function() { | |
| // TODO: Implement | |
| }; | |
| $(document).ready(function() { | |
| rh.color.enableButtons(); | |
| rh.color.updateView(); | |
| $("#input-red").focus(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment