Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<hexColor.length; i++) {
if(isNaN( parseInt(hexColor[i], 16) )) {
return;
}
}

var result = 'hello!';

var red = hexColor.substr(0, 2);
var green = hexColor.substr(2, 2);
var blue = hexColor.substr(4, 2);
var hexInt = parseInt(hexColor, 16);
var r = (hexInt & 0xff0000) >> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<hexColor.length; i++) {
if(isNaN( parseInt(hexColor[i], 16) )) {
return;
}
}

var result = 'hello!';

var red = hexColor.substr(0, 2);
var green = hexColor.substr(2, 2);
var blue = hexColor.substr(4, 2);
var hexInt = parseInt(hexColor, 16);
var r = (hexInt & 0xff0000) >> 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);
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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.

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
Expand Down