diff --git a/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-objc.lbaction/Contents/Scripts/hex2uicolor-objc.js b/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-objc.lbaction/Contents/Scripts/hex2uicolor-objc.js index 566eb8f..29ca16e 100644 --- a/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-objc.lbaction/Contents/Scripts/hex2uicolor-objc.js +++ b/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-objc.lbaction/Contents/Scripts/hex2uicolor-objc.js @@ -5,27 +5,41 @@ function run() { function runWithString(hexColor) { hexColor = hexColor.replace(/^#/, ''); - if (!hexColor.match(/^[0-9a-fA-F]{6,6}$/)) { + if (hexColor.length == 3) { + hexColor = hexColor[0] + hexColor[0] + hexColor[1] + hexColor[1] + hexColor[2] + hexColor[2]; + } + + // check for a valid length + if ((hexColor == "") || (hexColor.length > 7) || (hexColor.length < 6)) { return; } + // check for valid hex values + for (i=0; i> 16; + var g = (hexInt & 0x00ff00) >> 8; + var b = hexInt & 0x0000ff; + + r = r/255.0; + g = g/255.0; + b = b/255.0; - var floatFor = function(x) { - return '0x' + x.toUpperCase() + ' / 255.0'; + function round(value, decimals) { + return Number(Math.round(value+'e'+decimals)+'e-'+decimals); } - result = "[UIColor colorWithRed:" + floatFor(red) + - " green:" + floatFor(green) + - " blue:" + floatFor(blue) + - " alpha:1.0]"; + result = '[UIColor colorWithRed:' + round(r, 3) + ' green:' + round(g, 3) + ' blue:' + round(b, 3) + ' alpha:1.0]'; if (LaunchBar.options.commandKey) { LaunchBar.paste(result); } else { return result; } -} +} \ No newline at end of file diff --git a/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-swift.lbaction/Contents/Scripts/hex2uicolor-swift.js b/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-swift.lbaction/Contents/Scripts/hex2uicolor-swift.js index c95f6a5..d66135f 100644 --- a/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-swift.lbaction/Contents/Scripts/hex2uicolor-swift.js +++ b/Hex2UIColor.lbext/Contents/Resources/Actions/Hex2UIColor-swift.lbaction/Contents/Scripts/hex2uicolor-swift.js @@ -5,23 +5,40 @@ function run() { function runWithString(hexColor) { hexColor = hexColor.replace(/^#/, ''); - if (!hexColor.match(/^[0-9a-fA-F]{6,6}$/)) { + if (hexColor.length == 3) { + hexColor = hexColor[0] + hexColor[0] + hexColor[1] + hexColor[1] + hexColor[2] + hexColor[2]; + } + + // check for a valid length + if ((hexColor == "") || (hexColor.length > 7) || (hexColor.length < 6)) { return; } + // check for valid hex values + for (i=0; i> 16; + var g = (hexInt & 0x00ff00) >> 8; + var b = hexInt & 0x0000ff; + + r = r/255.0; + g = g/255.0; + b = b/255.0; - var floatFor = function(x) { - return '0x' + x.toUpperCase() + ' / 255.0'; + function round(value, decimals) { + return Number(Math.round(value+'e'+decimals)+'e-'+decimals); } - result = "UIColor(red:" + floatFor(red) + - ", green:" + floatFor(green) + - ", blue:" + floatFor(blue) + + result = "UIColor(red:" + round(r, 3) + + ", green:" + round(g, 3) + + ", blue:" + round(b, 3) + ", alpha:1.0)"; if (LaunchBar.options.commandKey) { LaunchBar.paste(result); diff --git a/README.md b/README.md index f476e94..2c9d35d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Hex2UIColor converts these pesky HTML Hex Color definitions kids seem to love so It will convert a simple `#663399` to a nice ```objc -[UIColor colorWithRed:0x66 / 255.0 green:0x33 / 255.0 blue:0x99 / 255.0 alpha:1.0] +[UIColor colorWithRed:0.4 green:0.2 blue:0.6 alpha:1] ``` When holding down the command key while hitting return the result will be pasted directly into the editor of your choice. @@ -15,7 +15,7 @@ When holding down the command key while hitting return the result will be pasted There is also a hipster version of this litte action that will convert your hex color into swift code: ```swift -UIColor(red:0x66 / 255.0, green:0x33 / 255.0, blue:0x99 / 255.0, alpha:1.0) +UIColor(red:0.4, green:0.2, blue:0.6, alpha:1.0) ``` ## Installation