Showing posts with label Color. Show all posts
Showing posts with label Color. Show all posts

Friday, August 28, 2009

Replacing Checkbox and Radio buttons

I found this pretty cool website (Hieuuk) that had an example of how to replace the checkbox and radio buttons with images. I was thinking about using it until I read the comments about how it doesn't work with the keyboard.

Well, I was inspired to mess around with the script, so I started working on it!

Here is a demo of the result of my work...



But sadly, I guess it was just a nice exercise for me because I have no idea how to make it into a jQuery plugin and well as I just found out... it's already been done, twice LOL. *Note to self* search the net before spending hours on a project o.O

The other versions:
  • I'm not sure, but this version looks like it was made to work with the jQuery UI

  • I really like this plugin version since it animates the radio button look... toggling a switch on and off. It just looks cool :P
Oh well, I don't want to say it was time wasted, but I still sort of like my version ;)

Tuesday, June 2, 2009

Convert jQuery RGB output to Hex Color

Please see my updated post:
Convert RGB(A) output to Hex Color


My current project uses Farbtastic which is a fantastic jQuery plugin which allows users to visually choose a color for an element. The problem I ran into was loading a default color from the element:

jQuery returns the element color in RGB format
document.write( $('#myElement').css('background-color') );
// outputs: rgb(34, 34, 34)

but Farbtastic uses the hex color format, just like the CSS.
#myElement { background-color: #222222 }
I found this really nice script (by R0bb13) which converts RGB to hex. This script requires the input variable to be in an array "[34,34,34]". In order to make the script use an RGB format "rgb(34,34,34)", I had to add one line to the script to remove the "rgb" and parenthesis and then convert (split) the result into an array...

Update #2 (allows entering an rgba value - opacity is ignored)

Updated (removing internal function):
//Function to convert hex format to a rgb color
function rgb2hex(orig){
 var rgb = orig.replace(/\s/g,'').match(/^rgba?\((\d+),(\d+),(\d+)/i);
 return (rgb && rgb.length === 4) ? "#" +
  ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : orig;
}
Here is an example of how to use this code with jQuery:
document.write( rgb2hex($('#myElement').css('background-color')) );
// outputs: #222222
Here is the original code from this post (not nearly as efficient as the updated script):
//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
  var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
  rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
  function hex(x) {
    return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  }
  return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

Monday, June 1, 2009

Design Resources

I haven't started working on the layout of my new blog, but I do have a list of design resources that I wanted to share! Hopefully you will find something useful here.