Skip to content

Commit 81321fa

Browse files
committed
Demo update / Readme update
- demo for sending data on every switch - demo for re-appending color picker for relative positioning - update of documentation
1 parent 0bfa3f1 commit 81321fa

3 files changed

Lines changed: 99 additions & 30 deletions

File tree

README.md

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,105 @@
22
# tinyColorPicker and colors
33

44
Looking for mobile first, tiny foot print, fast, scaleable, flexible, pluggable and a rich color model...<br>
5-
This small (4.9KB gZip, 10.8KB minified, no HTML, images or css needed) HSB colorpicker is based on a subset of [colors.js](https://github.com/PitPik/colorPicker/blob/master/colors.js) from it's big brother [colorPicker](https://github.com/PitPik/colorPicker/) for a precise and rich color model, supports all modern features like touch and MS pointer, GPU accelerated rendering, battery friendly requestAnimationFrame and provides a lot of hooks for developers to write plugins.
6-
7-
tinyColorPicker now supports AMD / CommonJS and bower.
5+
This small (5.07KB gZip, 11.39KB minified, no extra HTML, images or css needed) HSB colorpicker is based on a subset of [colors.js](https://github.com/PitPik/colorPicker/blob/master/colors.js) from it's big brother [colorPicker](https://github.com/PitPik/colorPicker/) for a precise and rich color model, supports all modern features like touch and MS pointer, GPU accelerated rendering, battery friendly requestAnimationFrame and provides a lot of hooks for developers to write plugins or extend the UI.
86

97
##Demo
108
See **demo** at [dematte.at/tinyColorPicker](http://dematte.at/tinyColorPicker)
119

1210
<img src="development/screen-shot-all.jpg" />
1311

14-
All the WCAG 2.0 calculations for readability are also based on opacity levels of all layers.<br>
15-
Supported color spaces are: rgb, hsv(b), hsl, HEX
12+
Supports WCAG 2.0 calculations for readability based on opacity levels of multiple layers.<br>
13+
Supported color spaces are: rgb, hsv(b), hsl, HEX.<br>
14+
CMYK, CMY, Lab and XYZ and more standards are supported if using [colors.js](https://github.com/PitPik/colorPicker/blob/master/colors.js) from [github.com/PitPik/colorPicker](https://github.com/PitPik/colorPicker/)
1615

1716
## Usage
1817

1918
```javascript
2019
<script type="text/javascript" src="jqColorPicker.min.js"></script>
2120
<script type="text/javascript">
22-
$('.color').colorPicker(); // that's it
23-
// $().colorPicker.destroy(); // for singlePageApps
21+
$('.color').colorPicker(/* optinal options */); // that's it
22+
</script>
23+
```
24+
```jqColorPicker.min.js``` holds all necessary data such as HTML, CSS and images in one file to run tinyColorPicker. So, it is not needed to include anything else than this single file.<br>
25+
If you need to debug things for development, you can also use ```colors.js```, the color calculation module, and ```jqColorPicker.js```, the UI and interaction module seperately.
26+
```javascript
27+
<script type="text/javascript" src="colors.js"></script>
28+
<script type="text/javascript" src="jqColorPicker.js"></script>
29+
<script type="text/javascript">
30+
$('.color').colorPicker();
2431
</script>
2532
```
2633

2734
## AMD / CommonJS wrapper
28-
tinyColorPicker now supports AMD and CommonJS import (thanks to [Munawwar](https://github.com/Munawwar)).
35+
tinyColorPicker now supports AMD and CommonJS import (thanks to [Munawwar](https://github.com/Munawwar)) in both, the minified version and the single fies (```colors.js``` and ```jqColorPicker.js```).
36+
37+
```javascript
38+
// example for requirejs configuration
39+
requirejs.config({
40+
baseUrl: 'scripts',
41+
paths: {
42+
jquery: 'lib/jquery-2.2.1.min'
43+
},
44+
shim: {
45+
'colorPicker': {
46+
deps: [ 'jquery' ],
47+
exports: 'jQuery.fn.colorPicker'
48+
}
49+
}
50+
});
2951

30-
## bower support
31-
tinyColorPicker can be received by bower:
52+
// then use tinyColorPicker in your module...
53+
(function (root, factory) {
54+
if (typeof define === 'function' && define.amd) {
55+
define(['jquery', 'jqColorPicker'], function (jQuery) {
56+
return factory(root, jQuery);
57+
});
58+
} else {
59+
factory(root, root.jQuery);
60+
}
61+
}(this, function(window, $){
62+
$('.color').colorPicker(options);
63+
}));
64+
```
65+
66+
## Bower support
67+
tinyColorPicker can be received via bower:
3268

3369
```javascript
3470
bower install tinyColorPicker
3571
```
3672

3773
## jqColorPicker.js
3874

39-
colorPicker uses an instance of Colors and passes the options to it, so some values are the same...
75+
```jqColorPicker.js``` is a jQuery plugin including the UI, CSS and images and uses an instance of Colors (from ```colors.js```) for all the color calculations. It passes the options to that instance, so some values might be the same when inspecting...
4076

4177
```javascript
4278
$('.color').colorPicker({
43-
color: ..., // see Colors...
44-
customBG: '#FFF' // see Colors...
79+
color: ..., // see Colors below...
80+
customBG: '#FFF' // see Colors below...
4581
animationSpeed: 150, // toggle animation speed
46-
GPU: true, // use transform: translate3d
47-
doRender: true | 'selector', // manipulate color and bgColor of input field (on certain elements if selector)
82+
GPU: true, // use transform: translate3d or regular rendereing (top, left)
83+
doRender: true | 'selector', // render color and bgColor of input field (on certain elements if selector)
4884
opacity: true, // enable / disable alpha slider
49-
renderCallback: function($elm, toggled) {}, // this === instance; $elm: the input field;toggle === true -> just appeared; false -> opposite; else -> is rendering on pointer move
50-
// toggled true/false can for example be used to check if the $elm has a certain className and then hide alpha,...
51-
buildCallback: function($elm) {}, // this === instance; $elm: the UI
52-
positionCallback: function($elm) {return {top: y, left: x}}, // this === instance; $elm: the trigger element;
53-
css: '', // replaces existing css
54-
cssAddon: '', // adds css to existing
85+
buildCallback: function($elm) {
86+
// 'this': colorPicker instance; // will be the same as in positionCallback() and renderCallback();
87+
// $elm: the UI (<div class="cp-color-picker"></div>)
88+
},
89+
renderCallback: function($elm, toggled) {
90+
// 'this': current colorPicker instance; // instance has all kinds of information about colorPicker such as $UI including dimensions etc...
91+
// $elm: the input field or other element that just toggled the colorPicker;
92+
// toggle -> 'true': just appeared; 'false': just closed; 'undefined': is rendering
93+
},
94+
positionCallback: function($elm) {
95+
// 'this': current colorPicker instance;
96+
// $elm: the input field or other element that just toggled the colorPicker;
97+
// optionally...
98+
return {top: y, left: x}; // positions colorPicker before appearing
99+
},
100+
css: '', // String: replaces existing css
101+
cssAddon: '', // String: adds css to existing
55102
margin: '', // positioning margin (can also be set in cssAddon)
56-
scrollResize: true // toggle for reposition colorPicker on window.resize/scroll
103+
scrollResize: true // toggle for repositioning colorPicker on window.resize/scroll
57104
gap: 4 // gap to right and bottom edge of view port if repositioned to fit
58105
dark: '#222' // default font color if background is light
59106
light: '#DDD' // default font color if background is dark
@@ -62,6 +109,8 @@ $('.color').colorPicker({
62109
forceAlpha: // force printing alpha channel (undefined = auto; false = never print alpha)
63110
});
64111
```
112+
See the following section or the demos on how the callbacks work and what you can do with them...
113+
65114
#### Some tips
66115

67116
The positionCallback can be used to optionally position the colorPicker different from its default; in case you want it to also show above or to the left of the input field etc.
@@ -70,23 +119,24 @@ If you return an object (```{left: x, top: y}``` to position the colorPicker) th
70119

71120
```javascript
72121
positionCallback: function($elm) {
73-
var _$UI = this.$UI, // this is the instance; this.$UI is the colorPicker DOMElement
122+
var $UI = this.$UI, // this is the instance; this.$UI is the colorPicker DOMElement
74123
position = $elm.offset(), // $elm is the current trigger that opened the UI
75124
gap = this.color.options.gap, // this.color.options stores all options
76125
top = 0,
77126
left = 0;
78127

79-
// do here your calculations with top and left and...
128+
// $UI.appendTo('#somwhereElse');
129+
// do here your calculations with top and left and then...
80130
return { // the object will be used as in $('.something').css({...});
81131
left: left,
82132
top: top
83133
}
84134
}
85135
```
86-
This callback is also good if you need to append your colorPicker to a different element then document.body. This way you can calculate the coordinates relative to the appended container...
136+
This callback is also good if you need to append your colorPicker to a different container than document.body. This way you can then calculate the coordinates relative to the appended container...
87137

88-
The renderCallback can be used as openCallback and closeCallback:
89138

139+
The renderCallback can be used as openCallback and closeCallback:
90140
```javascript
91141
renderCallback: function($elm, toggled) {
92142
if (toggled === true) { // simple, lightweight check
@@ -105,7 +155,7 @@ this.$UI.find('.cp-alpha').toggle(!$elm.hasClass('no-alpha'));
105155

106156
## colors.js
107157

108-
This section only shows the options for color.js. They are picked up automatically if set in $('.color').colorPicker
158+
This section only shows the options for color.js. They are picked up automatically if set in ```$('.color').colorPicker({/* options */});```
109159

110160
```javascript
111161
Colors({ // all options have a default value...

demo/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ <h1>Tiny jQuery colorPicker</h1>
2424
<p>This is a demo that describes tinyColorPicker custumization 'Skinned dev-tools like with RGB sliders' in a more understandable way.</p>
2525
<a name="demo" id="demo" class="a-inline"></a>
2626
<h2>Skinned dev-tools like, with RGB sliders</h2>
27-
<div class="input-toggles">
27+
<div class="input-toggles wrapper">
2828
<input class="color" value="#B6BD79" />
2929
<input class="color no-alpha" value="rgb(162, 63, 3)" />
3030
<input class="color no-sliders" value="hsl(32, 95%, 23%)" />
3131
</div>
32-
<div class="div-toggles">
32+
<div class="div-toggles wrapper">
3333
<div class="trigger" value="#556B2F"><div><div></div></div></div>
3434
<div class="trigger" value="rgb(100, 86, 70)"><div><div></div></div></div>
3535
<div class="trigger" value="hsla(167, 29%, 68%, 0.8)"><div><div></div></div></div>

demo/index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $(function(){
6969
$window = $(window),
7070
gap = this.color.options.gap; // this.color.options stores all options
7171

72-
// _$UI.appendTo('#content-wrapper'); // demonstration only
72+
// _$UI.appendTo($elm.closest('.wrapper').eq(0)); // demonstration only
7373

7474
return { // this demo is a copy of the internal usage (to show how it works);
7575
'left': (_$UI._left = position.left) -
@@ -85,6 +85,8 @@ $(function(){
8585
renderCallback: function($elm, toggled) {
8686
var colors = this.color.colors; // the whole color object
8787
var rgb = colors.RND.rgb; // the RGB color in 0-255
88+
var oldValue = '';
89+
var currentValue = '';
8890

8991
// the following 6 lines are not necessary if you don't have the trigger icons with the arrows...
9092
// if (toggled === true) { // just showing (only on show)
@@ -99,6 +101,23 @@ $(function(){
99101
// $elm.css({'color': 'transparent'});
100102
// }
101103

104+
// following section (13 lines) show how to get values on every switch to an other
105+
// input field and on close...
106+
/*
107+
if (toggled === true) { // this happens on open (first time or when switching to another one)
108+
if (this.$oldElement && this.$oldElement[0] !== $elm[0]) { // previously closed while opening this one
109+
currentValue = this.color.toString(); // store current value
110+
oldValue = this.$oldElement.val(); // actual value of previous element
111+
oldValue = this.color.setColor(oldValue); // set color to that old value
112+
console.log(this.color.toString()); // show color of previously opened in rgba mode
113+
this.color.setColor(currentValue); // set it back to normal
114+
}
115+
this.$oldElement = $elm; // store for next switch...
116+
} else if (toggled === false) { // this happens on close (only)
117+
console.log(this.color.toString()); // show color model of just closed
118+
this.$oldElement = null; // delete $oldElement as there is no other swich possible
119+
}
120+
*/
102121
if (toggled === true) { // on show colorPicker
103122
this.$alpha.toggle(!$elm.hasClass('no-alpha'));
104123
this.$sliders.toggle(!$elm.hasClass('no-sliders'));

0 commit comments

Comments
 (0)