Skip to content

Commit 7c2e2e9

Browse files
committed
First draft of color mixingw
1 parent 21964fc commit 7c2e2e9

File tree

3 files changed

+253
-0
lines changed

3 files changed

+253
-0
lines changed

dojo/colormix.css

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* === General === */
2+
/* HTML5 elements */
3+
article, aside, figure, footer, header, hgroup,
4+
menu, nav, section { display: block; }
5+
6+
body {
7+
background-color: #73B4FF;
8+
}
9+
10+
h1, h2, h3, h4, h5, h6 {
11+
margin: 0px 0px 10px 0px;
12+
}
13+
14+
h1 { font-size: 3em; font-weight: bolder; }
15+
16+
/* === Specific elements === */
17+
#wrap {
18+
/* Mmm, borders */
19+
-webkit-border-radius: 10px;
20+
-moz-border-radius: 10px;
21+
border-radius: 10px;
22+
/* Drop shadows too */
23+
webkit-box-shadow: 5px 5px 5px #474747;
24+
-moz-box-shadow: 5px 5px 5px #474747;
25+
box-shadow: 5px 5px 5px #474747;
26+
/* Other more boring stuff */
27+
background-color: #FFF;
28+
margin: 10px auto;
29+
width: 960px;
30+
padding: 10px;
31+
}
32+
33+
.colorwrap {
34+
/* Clears floats, I hear */
35+
overflow: hidden;
36+
}
37+
38+
.colorpane {
39+
display: block;
40+
float: left;
41+
width: 206px;
42+
height: 100px;
43+
border: 1px solid black;
44+
margin: 5px;
45+
font-size: 20px;
46+
font-weight: bolder;
47+
padding-top: 55px;
48+
text-align: center;
49+
}
50+
51+
.dijitColorPalette {
52+
float: left;
53+
margin: 5px;
54+
}
55+
56+
#slider {
57+
float: left;
58+
margin: 5px 5px 5px 220px;
59+
width: 433px;
60+
}
61+
62+
#resultCol {
63+
margin-left: 223px;
64+
width: 424px;
65+
}
66+
67+
/* vim: set sw=4 ts=4 noet: */

dojo/colormix.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Color Mixer</title>
6+
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/resources/dojo.css">
7+
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css">
8+
<link rel="stylesheet" type="text/css" href="colormix.css">
9+
</head>
10+
<body class="claro">
11+
<div id="wrap">
12+
<header>
13+
<h1>Color Mixer</h1>
14+
</header>
15+
<section id="loading">
16+
Loading, please wait...
17+
</section>
18+
<section id="content" style="display: none;">
19+
<div id="colors" class="colorwrap">
20+
<span id="leftPal" class="colorpane"> </span>
21+
<span id="leftCol" class="colorpane"> </span>
22+
<span id="rightCol" class="colorpane"> </span>
23+
<span id="rightPal" class="colorpane"> </span>
24+
</div>
25+
<div class="colorwrap">
26+
<span id="slider"> </span>
27+
</div>
28+
<div class="colorwrap">
29+
<span id="resultCol" class="colorpane"> </span>
30+
</div>
31+
</section>
32+
<footer>
33+
<p>All content on this page copyright &copy; 2010 Brian Arnold, released under an MIT license, so, do whatever you want with it.</p>
34+
</footer>
35+
</div>
36+
<!-- Scripts! -->
37+
<script type="text/javascript">
38+
// Dojo Config -- MUST be before Dojo comes in
39+
var djConfig = {
40+
parseOnLoad: true,
41+
isDebug: true
42+
};
43+
</script>
44+
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js"></script>
45+
<script src="colormix.js"></script>
46+
<!-- Analytics -->
47+
<script type="text/javascript">
48+
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
49+
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
50+
</script>
51+
<script type="text/javascript">
52+
try {
53+
var pageTracker = _gat._getTracker("UA-279164-1");
54+
pageTracker._trackPageview();
55+
} catch(err) {}
56+
</script>
57+
</body>
58+
</html>
59+
<!-- vim: set sw=4 ts=4 noet: -->

dojo/colormix.js

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Color Mixer
2+
// Brian Arnold
3+
// MIT licensed, though I doubt you'd actually want to take any of it yet.
4+
5+
// Dojo Requires
6+
dojo.require('dojo.Stateful');
7+
dojo.require('dijit.ColorPalette');
8+
dojo.require('dijit.form.HorizontalSlider');
9+
10+
// Variables
11+
// (I realize global isn't always great, but this is just a simple demo)
12+
var left, right, slider, result;
13+
14+
// Functions
15+
16+
// rndCol: Just a simple way to get a random color
17+
function rndCol() {
18+
var i, cols = new Array(3);
19+
20+
for (i = 0; i < 3; i++) {
21+
cols[i] = Math.floor(Math.random() * 256);
22+
}
23+
24+
return dojo.colorFromArray(cols);
25+
}
26+
27+
// colChange: Will be used in a watch callback for left and right colors
28+
function colChange(prop, oldCol, newCol) {
29+
// Set the node's background
30+
var node = this.get('displayNode'), newColHex = newCol.toHex();
31+
console.log('colChange', this, prop, oldCol, newCol);
32+
33+
// If we have the same color, we can just get out
34+
if (oldCol && newCol && oldCol.toHex() === newColHex) return;
35+
36+
// If we have a node, work it
37+
if (node) {
38+
node.innerHTML = newColHex
39+
node.style.backgroundColor = newColHex;
40+
}
41+
}
42+
43+
// colChangeAndMix: A slight tweak to not just change, but kick off a mix
44+
function colChangeAndMix(prop, oldCol, newCol) {
45+
// Run the colChange first
46+
colChange.apply(this, arguments);
47+
48+
// Kick off a mix
49+
mixCols(slider.get('value'));
50+
}
51+
52+
// mixCols: Just something to mix the two colors if they exist
53+
function mixCols(val) {
54+
var leftCol = left.get('color'),
55+
rightCol = right.get('color');
56+
57+
console.log('mixCols()');
58+
if (!leftCol || !rightCol) {
59+
// Just dump out for now
60+
return;
61+
}
62+
63+
// TODO Animate
64+
result.set('color', dojo.blendColors(leftCol, rightCol, val));
65+
}
66+
67+
// Ready, do somethin'
68+
dojo.ready(function(){
69+
// Set up a few Stateful items
70+
left = new dojo.Stateful({
71+
color: null,
72+
palette: new dijit.ColorPalette({}, 'leftPal'),
73+
paletteNode: dojo.byId('leftPal'),
74+
displayNode: dojo.byId('leftCol')
75+
});
76+
right = new dojo.Stateful({
77+
color: null,
78+
palette: new dijit.ColorPalette({}, 'rightPal'),
79+
paletteNode: dojo.byId('rightPal'),
80+
displayNode: dojo.byId('rightCol')
81+
});
82+
result = new dojo.Stateful({
83+
color: null,
84+
displayNode: dojo.byId('resultCol')
85+
});
86+
87+
// Build a slider
88+
slider = new dijit.form.HorizontalSlider({
89+
value: 0.5,
90+
minimum: 0,
91+
maximum: 1,
92+
//intermediateChanges: true,
93+
onChange: mixCols
94+
}, 'slider');
95+
96+
// Set up some events
97+
left.watch('color', colChangeAndMix);
98+
right.watch('color', colChangeAndMix);
99+
result.watch('color', colChange);
100+
101+
// Tweak the left and right
102+
dojo.forEach([left, right], function (item, i) {
103+
var pal = item.get('palette'),
104+
palNode = item.get('paletteNode');
105+
disp = item.get('displayNode');
106+
107+
// Mmm, events
108+
dojo.connect(disp, "onclick", function () {
109+
palNode.style.visibility = 'visible';
110+
});
111+
dojo.connect(pal, 'onChange', function (col) {
112+
palNode.style.visibility = 'hidden';
113+
item.set('color', dojo.colorFromHex(col));
114+
});
115+
116+
// Set up some initial display
117+
palNode.style.visibility = 'hidden';
118+
item.set('color', rndCol());
119+
});
120+
121+
// Show it all
122+
dojo.byId('loading').style.display = 'none';
123+
dojo.byId('content').style.display = '';
124+
}); // dojo.ready
125+
126+
// Vim settings, whee
127+
// vim: set sw=4 ts=4 noet:

0 commit comments

Comments
 (0)