Skip to content

Instantly share code, notes, and snippets.

@dimitris-c
Last active August 29, 2015 14:13
Show Gist options
  • Save dimitris-c/0317d830def2cd6027e5 to your computer and use it in GitHub Desktop.
Save dimitris-c/0317d830def2cd6027e5 to your computer and use it in GitHub Desktop.
mymqwm
<h1>ARGB color to Objective-C UIColor</h1><br>
<div id="input-bar">
<div id="hexBar">
<input type="text" id="argbText"></input>
</div>
<div id="button">
<input type="button" id="convertButton" value="Convert" onclick="argb2UIColor()"></input>
</div>
</div>
<div id="output-bar">
<div id="objcBar"></div>
</div>
function argb2UIColor () {
var argb = document.getElementById("argbText").value;
var argbInt = parseInt(argb, 16);
var blue = argbInt & 0xff;
var green = argbInt >> 8 & 0xff;
var red = argbInt >> 16 & 0xff;
var alpha = argbInt >> 24 & 0xff;
//alpha = (alpha / 255.0).toFixed(2);
iOSRGB = '[UIColor colorWithRed:' + red + '.0/255.0 green:' + green + '.0/255.0 blue:' + blue + '.0/255.0 alpha:'+ alpha +'.0/255.0]';
document.getElementById("objcBar").style.background = '#' + componentToHex(argb);
document.getElementById("objcBar").innerHTML = iOSRGB;
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function hex2rgb(value) {
var hex = document.getElementById("argbText").value;
if (hex.length == 4 || hex.length == 7)
if (hex[0] == "#")
hex = hex.substring(1);
else {
window.alert("Please enter valid a hex color value.");
return;
}
if (hex.length == 3)
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
// check for a valid length
if ((hex == "") || (hex.length > 7) || (hex.length < 6)) {
window.alert("Please enter valid a hex color value.");
return;
}
// check for valid hex values
for (i=0; i<hex.length; i++) {
if(isNaN( parseInt(hex[i], 16) )) {
window.alert("Please enter valid a hex color value.");
return;
}
}
// convert hex to individual ints
var hexInt = parseInt(hex, 16);
var r = (hexInt & 0xff0000) >> 16;
var g = (hexInt & 0x00ff00) >> 8;
var b = hexInt & 0x0000ff;
// print cgcolor string to screen
iOSRGB = '[UIColor colorWithRed:' + r + '.0/255.0 green:' + g + '.0/255.0 blue:' + b + '.0/255.0 alpha:1.0]';
document.getElementById("objcBar").style.background = '#' + hex;
document.getElementById("objcBar").innerHTML = iOSRGB;
}
body {
font-family: Futura, "Trebuchet MS", Arial, sans-serif;
text-align:center;
}
input:focus,
button:focus {
outline:none;
}
#input-bar {
position:relative;
height:35px;
display:inline-block;
}
#hexBar {
display:inline-block;
float:left;
height:100%;
margin:0;
background:#9cf;
}
#argbText {
margin:0 5px;
border:0;
font-size:20px;
line-height:33px;
background:#9cf;
color:#222;
}
#button {
height:100%;
float:left;
display:inline-block;
margin:0;
background:#222;
cursor:pointer;
}
#convertButton {
height:100%;
margin:0;
border:0;
output:none;
background:#7af;
//opacity:0.8;
cursor:pointer;
}
#convertButton:active {
background:#59f;
//opacity:0.7;
}
#objcBar {
display:inline-block;
padding:5px;
//background:#9af;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment