UvumiTools ColorSphere Plugin

Description

We can't take all the credit for this one. It is heavily inspired by Color Jack's color sphere, but rewritten, improved, optimized and simplified for the Mootools Javascript Framework.

We needed a color picker with the following features :

Features

Requirements

What you'll need:

How To

Initialization

new UvumiSphere(selector,options);

Arguments

Options

Implementation

First, you simply import the files provided in the zip archive into your document. While you can insert <script> tags anywhere, we recommend you put them inside the <head> tag of your document:

<head>
<!-- Insert these lines in your document's head -->
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-sphere.css"/>
<script type="text/javascript" src="js/mootools-for-sphere.js"> </script>
<script type="text/javascript" src="js/UvumiSphere.js"> </script>
</head>

Then you must prepare the HTML code for your ColorSphere. For one color, all it requires is a single text input. Let's say we want the color to be set to pure red the first time you open the picker. So we set the input value to "#f00". We suppose the reason you pick a color is to submit it to a server, so we put this input inside a form.

<form id="color-form" action="submit-color.php" method="post" >
<p>
<!-- This is our color input -->
<input id="color-input" name="color" type="text" value="#F00" />
</p>
<p>
<button type="submit">Send</button>
</p>
</form>

So far we have a form that will send a value named 'color' as a 'POST' parameter to the script 'submit-color.php', which is a name we just made up. It will be your job to write a server-side script to catch this value and do something useful with it. The value for 'color' is '#F00' unless the user types in something else. Techinically it is functional and could totaly work as it is.

Then we are ready to initialize the sphere script. By default, the sphere recieves only one argument, the selector. The selector is a string that works like a CSS rule. Using the exact same syntax, it will tell the script which HTML elements it should apply to. In our case there is only one input, so we are going to use a '#' for id selection, but you can create more complex rules, using tags and class selections. If you are familiar with CSS styling, this should not be a problem for you.

<!-- We create a CSS selector that match our color input's id.
Here we added 'input' to make more obvious,
but since an id is supposed to unique,
'#color-input' would have been enough. -->

<script type="text/javascript">
new UvumiSphere('input#color-input');
</script>

In the end, we put everything together. Mootools requires a strict Doctype to work properly. We get something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="css/uvumi-sphere.css"/>
<script type="text/javascript" src="js/mootools-for-sphere.js"> </script>
<script type="text/javascript" src="js/UvumiSphere.js"> </script>
<script type="text/javascript">
new UvumiSphere('input#color-input');
</script>
</head>
<body>
<div>
<form id="color-form" action="submit-color.php" method="post" >
<p>
<input id="color-input" name="color" type="text" value="#F00" />
</p>
<p>
<button type="submit">Send</button>
</p>
</form>
</div>
</body>
</html>

Technically speaking, that should work. The text input will be virtually replaced with a 'pick' button. Virtally, because the input still exists, it is simply hidden. It will be constantly updated, and submited along with the form.

This is for the default, simple usage. We are going to see how to apply the sphere to several inputs. Let's say we have four color inputs this time, all needing the ColorSphere.
We saw how to use an ID selector. We could use an ID selector again, by just listing the inputs IDs, separated by commas: #input1,#inpu2,#input3,#input4. It will work, but we can use any CSS rule (including advanced CSS3 rules that are not even supported by all browsers).
We could use a rule to apply the sphere to any input inside the form: form#color-form input . Problem is, this will turn every input tag into color pickers, even if they are checkboxes or radio buttons. Probably not what we want.
The best way to filter the inputs we want is to use the class property: we add a class to the inputs we want to apply the sphere to, and we use this class in the selector, thanks to the dot character:

<form id="color-form" action="submit-color.php" method="post" >
<p>
<input class="color-input" name="color1" type="text" value="#F00" />
<input class="color-input" name="color2" type="text" value="#0F0" />
<input class="color-input" name="color3" type="text" value="#00F" />
</p>
<p>
<button type="submit">Send</button>
</p>
</form>

<!-- the dot before color-input is not a typo, this is what define the selection rule by class.
This means "select the element with class color-input"-->

<script type="text/javascript">
new UvumiSphere('.color-input');
</script>

If we have two forms, and we went a separate picker for each, we can create two instances (eventually with different options), and specify in the css rules which form they should affect:

<!-- Here we create two ColorSphere instances, one for each form,
using the forms' ID to differenciation the selectors.
The two instances also have different options:
In the first form, inputs don't have values, so we set a defaut color to #f00 (red)
In the second form, we want the buttons to say 'Select',
So we set the buttonText option accordingly. -->

<script type="text/javascript">
new UvumiSphere('form#color-form1 input.color-input',{defaultColor:'#f00'});
new UvumiSphere('form#color-form2 input.color-input',{buttonText:'Select'});
</script>
<form id="color-form1" action="submit-color.php" method="post" >
<p>
<input class="color-input" name="color1" type="text" />
<input class="color-input" name="color2" type="text" />
</p>
<p>
<button type="submit">Send</button>
</p>
</form> <form id="color-form2" action="submit-color.php" method="post" >
<p>
<input class="color-input" name="color1" type="text" value="#F00" />
<input class="color-input" name="color2" type="text" value="#0F0" />
</p>
<p>
<button type="submit">Send</button>
</p>
</form>

Known Issues

Download

Change Log