Skip to content

Commit bd230e4

Browse files
committed
new method toString / optimization
Added method ‚toString‘ to colors.js; Converts color to HTML-string with auto or forced alpha. Some minor optimizations.
1 parent 777b22a commit bd230e4

File tree

12 files changed

+92
-66
lines changed

12 files changed

+92
-66
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Peter Dematté
3+
Copyright (c) 2016 Peter Dematté
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ After initializing Color or ColorPicker you'll get a clean but rhich model of th
9999
myColors: {
100100
colors: { all kinds of color values... see later},
101101
options: { all the options you set or that are set as default... },
102-
__proto__: { // all methods Color uses
102+
__proto__: { // all methods Color uses (See https://github.com/PitPik/colorPicker for details)
103103
setColor: function(newCol, type, alpha) {},
104104
setCustomBackground: function(col) {},
105105
saveAsBackground: function() {},
106+
// new method: converts current color to HTML-String like: rgba(123, 234, 0, 0.89)
107+
// forceAlpha === true / false -> alway / never print alpha, === undefined -> auto
108+
toString: function('rgb' || 'hsl' || 'hex' || '' -> 'rgb', forceAlpha) {},
106109
}
107110
}
108111
```

colors.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
HEX: {HEX: [0, 16777215]} // maybe we don't need this
2020
},
2121

22+
_Math = window.Math,
23+
_round = _Math.round,
24+
2225
_instance = {},
2326
_colors = {},
2427

@@ -83,6 +86,10 @@
8386
return setColor(this.colors, undefined, 'rgb', true);
8487
};
8588

89+
Colors.prototype.toString = function(colorMode, forceAlpha) {
90+
return ColorConverter.color2text(colorMode || 'rgba', this.colors, forceAlpha);
91+
};
92+
8693
// ------------------------------------------------------ //
8794
// ---------- Color calculation related stuff ---------- //
8895
// -------------------------------------------------------//
@@ -112,7 +119,7 @@
112119
color.rgb = {r: rgb.r, g: rgb.g, b: rgb.b};
113120
color.alpha = alpha;
114121
// color.RGBLuminance = getLuminance(RGB);
115-
color.equivalentGrey = Math.round(grey.r * RGB.r + grey.g * RGB.g + grey.b * RGB.b);
122+
color.equivalentGrey = _round(grey.r * RGB.r + grey.g * RGB.g + grey.b * RGB.b);
116123

117124
color.rgbaMixBlack = mixColors(rgb, {r: 0, g: 0, b: 0}, alpha, 1);
118125
color.rgbaMixWhite = mixColors(rgb, {r: 1, g: 1, b: 1}, alpha, 1);
@@ -151,7 +158,7 @@
151158
if (!RND[typ]) RND[typ] = {};
152159
modes = colors[typ];
153160
for(mode in modes) {
154-
RND[typ][mode] = Math.round(modes[mode] * ranges[typ][mode][1]);
161+
RND[typ][mode] = _round(modes[mode] * ranges[typ][mode][1]);
155162
}
156163
}
157164
}
@@ -198,7 +205,7 @@
198205
background.rgbaMixCustom[luminance]);
199206
colors.rgbaMixBGMixCustom = rgbaMixBGMixCustom;
200207
/* ------ */
201-
rgbaMixBGMixCustom.luminanceDelta = Math.abs(
208+
rgbaMixBGMixCustom.luminanceDelta = _Math.abs(
202209
rgbaMixBGMixCustom[luminance] - background.rgbaMixCustom[luminance]);
203210
rgbaMixBGMixCustom.hueDelta = getHueDelta(background.rgbaMixCustom, rgbaMixBGMixCustom, true);
204211
/* ------ */
@@ -246,6 +253,25 @@
246253
return color;
247254
},
248255

256+
color2text: function(colorMode, colors, forceAlpha) {
257+
var alpha = forceAlpha !== false && _round(colors.alpha * 100) / 100,
258+
hasAlpha = typeof alpha === 'number' &&
259+
forceAlpha !== false && (forceAlpha || alpha !== 1),
260+
RGB = colors.RND.rgb,
261+
HSL = colors.RND.hsl,
262+
mode = colorMode.toLowerCase().substr(0, 3),
263+
shouldBeHex = mode === 'hex' && hasAlpha,
264+
isHex = mode === 'hex' && !shouldBeHex,
265+
isRgb = mode === 'rgb' || shouldBeHex,
266+
innerText = isRgb ? RGB.r + ', ' + RGB.g + ', ' + RGB.b :
267+
HSL.h + ', ' + HSL.s + '%, ' + HSL.l + '%',
268+
text = isHex ? '#' + colors.HEX : (shouldBeHex ? 'rgb' : mode) +
269+
(hasAlpha ? 'a' : '') + '(' + innerText +
270+
(hasAlpha ? ', ' + alpha : '') + ')';
271+
272+
return text;
273+
},
274+
249275
RGB2HEX: function(RGB) {
250276
return (
251277
(RGB.r < 16 ? '0' : '') + RGB.r.toString(16) +
@@ -257,21 +283,21 @@
257283
HEX2rgb: function(HEX) {
258284
HEX = HEX.split(''); // IE7
259285
return {
260-
r: parseInt(HEX[0] + HEX[HEX[3] ? 1 : 0], 16) / 255,
261-
g: parseInt(HEX[HEX[3] ? 2 : 1] + (HEX[3] || HEX[1]), 16) / 255,
262-
b: parseInt((HEX[4] || HEX[2]) + (HEX[5] || HEX[2]), 16) / 255
286+
r: +('0x' + HEX[0] + HEX[HEX[3] ? 1 : 0]) / 255,
287+
g: +('0x' + HEX[HEX[3] ? 2 : 1] + (HEX[3] || HEX[1])) / 255,
288+
b: +('0x' + (HEX[4] || HEX[2]) + (HEX[5] || HEX[2])) / 255
263289
};
264290
},
265291

266292
hue2RGB: function(hue) {
267293
var h = hue * 6,
268-
mod = ~~h % 6, // Math.floor(h) -> faster in most browsers
294+
mod = ~~h % 6, // _Math.floor(h) -> faster in most browsers
269295
i = h === 6 ? 0 : (h - mod);
270296

271297
return {
272-
r: Math.round([1, 1 - i, 0, 0, i, 1][mod] * 255),
273-
g: Math.round([i, 1, 1, 1 - i, 0, 0][mod] * 255),
274-
b: Math.round([0, 0, i, 1, 1, 1 - i][mod] * 255)
298+
r: _round([1, 1 - i, 0, 0, i, 1][mod] * 255),
299+
g: _round([i, 1, 1, 1 - i, 0, 0][mod] * 255),
300+
b: _round([0, 0, i, 1, 1, 1 - i][mod] * 255)
275301
};
276302
},
277303

@@ -291,13 +317,13 @@
291317
if (r < g) {
292318
r = g + (g = r, 0);
293319
k = -2 / 6 - k;
294-
min = Math.min(g, b); // g < b ? g : b; ???
320+
min = _Math.min(g, b); // g < b ? g : b; ???
295321
}
296322
chroma = r - min;
297323
s = r ? (chroma / r) : 0;
298324
return {
299325
h: s < 1e-15 ? ((_colors && _colors.hsl && _colors.hsl.h) || 0) :
300-
chroma ? Math.abs(k + (g - b) / (6 * chroma)) : 0,
326+
chroma ? _Math.abs(k + (g - b) / (6 * chroma)) : 0,
301327
s: r ? (chroma / r) : ((_colors && _colors.hsv && _colors.hsv.s) || 0), // ??_colors.hsv.s || 0
302328
v: r
303329
};
@@ -307,7 +333,7 @@
307333
var h = hsv.h * 6,
308334
s = hsv.s,
309335
v = hsv.v,
310-
i = ~~h, // Math.floor(h) -> faster in most browsers
336+
i = ~~h, // _Math.floor(h) -> faster in most browsers
311337
f = h - i,
312338
p = v * (1 - s),
313339
q = v * (1 - f * s),
@@ -349,7 +375,7 @@
349375
v = l < 0.5 ? l * (1 + s) : (l + s) - (s * l),
350376
m = l + l - v,
351377
sv = v ? ((v - m) / v) : 0,
352-
sextant = ~~h, // Math.floor(h) -> faster in most browsers
378+
sextant = ~~h, // _Math.floor(h) -> faster in most browsers
353379
fract = h - sextant,
354380
vsf = v * sv * fract,
355381
t = m + vsf,
@@ -381,9 +407,9 @@
381407
}
382408

383409
function getHueDelta(rgb1, rgb2, nominal) {
384-
return (Math.max(rgb1.r - rgb2.r, rgb2.r - rgb1.r) +
385-
Math.max(rgb1.g - rgb2.g, rgb2.g - rgb1.g) +
386-
Math.max(rgb1.b - rgb2.b, rgb2.b - rgb1.b)) * (nominal ? 255 : 1) / 765;
410+
return (_Math.max(rgb1.r - rgb2.r, rgb2.r - rgb1.r) +
411+
_Math.max(rgb1.g - rgb2.g, rgb2.g - rgb1.g) +
412+
_Math.max(rgb1.b - rgb2.b, rgb2.b - rgb1.b)) * (nominal ? 255 : 1) / 765;
387413
}
388414

389415
function getLuminance(rgb, normalized) {
@@ -392,7 +418,7 @@
392418
luminance = _instance.options.luminance;
393419

394420
for (var i = RGB.length; i--; ) {
395-
RGB[i] = RGB[i] <= 0.03928 ? RGB[i] / 12.92 : Math.pow(((RGB[i] + 0.055) / 1.055), 2.4);
421+
RGB[i] = RGB[i] <= 0.03928 ? RGB[i] / 12.92 : _Math.pow(((RGB[i] + 0.055) / 1.055), 2.4);
396422
}
397423
return ((luminance.r * RGB[0]) + (luminance.g * RGB[1]) + (luminance.b * RGB[2]));
398424
}
@@ -418,11 +444,11 @@
418444
} else {
419445
ratio = (lum2 + 0.05) / (lum1 + 0.05);
420446
}
421-
return Math.round(ratio * 100) / 100;
447+
return _round(ratio * 100) / 100;
422448
}
423449

424450
function limitValue(value, min, max) {
425-
// return Math.max(min, Math.min(max, value)); // faster??
451+
// return _Math.max(min, _Math.min(max, value)); // faster??
426452
return (value > max ? max : value < min ? min : value);
427453
}
428454

demo/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<link rel="stylesheet" type="text/css" href="../index.css">
1212
<link id="colorPickerMod" rel="stylesheet" type="text/css" href="mod.css">
1313
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
14-
<script type="text/javascript" src="../jqColorPicker.min.js"></script>
15-
<!-- <script type="text/javascript" src="../colors.js"></script> -->
16-
<!-- <script type="text/javascript" src="../jqColorPicker.js"></script> -->
14+
<!-- <script type="text/javascript" src="../jqColorPicker.min.js"></script> -->
15+
<script type="text/javascript" src="../colors.js"></script>
16+
<script type="text/javascript" src="../jqColorPicker.js"></script>
1717
<script type="text/javascript" src="index.js"></script>
1818

1919
<title>tiny jQuery color picker demo</title>
@@ -25,9 +25,9 @@ <h1>Tiny jQuery colorPicker</h1>
2525
<a name="demo" id="demo" class="a-inline"></a>
2626
<h2>Skinned dev-tools like, with RGB sliders</h2>
2727
<div class="input-toggles">
28-
<input class="color no-alpha" value="#B6BD79" />
28+
<input class="color" value="#B6BD79" />
2929
<input class="color no-alpha" value="rgb(162, 63, 3)" />
30-
<input class="color no-alpha" value="hsl(32, 95%, 23%)" />
30+
<input class="color no-sliders" value="hsl(32, 95%, 23%)" />
3131
</div>
3232
<div class="div-toggles">
3333
<div class="trigger" value="#556B2F"><div><div></div></div></div>

demo/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ $(function(){
3636
'<div class="cp-rgb-b"><div class="cp-rgb-b-cursor"></div></div>' +
3737
'<div class="cp-patch"><div></div></div><div class="cp-disp"></div>');
3838

39-
this.cursorRStyle = $elm.find('.cp-rgb-r-cursor')[0].style; // caching for faster render renderCallback
40-
this.cursorGStyle = $elm.find('.cp-rgb-g-cursor')[0].style;
41-
this.cursorBStyle = $elm.find('.cp-rgb-b-cursor')[0].style;
39+
this.$sliders = $elm.find('.cp-rgb-r, .cp-rgb-g, .cp-rgb-b');
40+
this.cursorRStyle = this.$sliders.find('.cp-rgb-r-cursor')[0].style; // caching for faster render renderCallback
41+
this.cursorGStyle = this.$sliders.find('.cp-rgb-g-cursor')[0].style;
42+
this.cursorBStyle = this.$sliders.find('.cp-rgb-b-cursor')[0].style;
4243

4344
this.patchStyle = $('.cp-patch div')[0].style;
4445
this.$display = $('.cp-disp');
46+
this.$alpha = $elm.find('.cp-alpha');
4547

4648
$elm.on('mousedown', '.cp-rgb-r, .cp-rgb-g, .cp-rgb-b', function(e) { // event delegation
4749
$currentSlider = $(this); // well ;o)
@@ -73,6 +75,11 @@ $(function(){
7375
// $elm.closest('.trigger').removeClass('active');
7476
// }
7577

78+
if (toggled === true) { // on show colorPicker
79+
this.$alpha.toggle(!$elm.hasClass('no-alpha'));
80+
this.$sliders.toggle(!$elm.hasClass('no-sliders'));
81+
}
82+
7683
this.patchStyle.backgroundColor = $elm[0].style.backgroundColor; // set patch color...
7784
this.$display.text(this.color.options.colorNames[colors.HEX] || $elm.val()); // ...and text aside
7885

demo/mod.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
.cp-color-picker .cp-rgb-r,
5858
.cp-color-picker .cp-rgb-g,
5959
.cp-color-picker .cp-rgb-b {
60+
clear: both;
6061
overflow: visible;
6162
width: 152px;
6263
margin: 12px 0 0;

index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<link rel="icon" type="image/x-icon" href="development/favicon.ico">
1313
<!-- <link rel="stylesheet" type="text/css" href="development/colorPicker.css"> -->
1414
<link rel="stylesheet" type="text/css" href="development/compatibility.css">
15+
16+
<!-- <script type="text/javascript" src="jquery-1.11.2.js"></script> -->
17+
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
18+
<script type="text/javascript" src="colors.js"></script>
19+
<script type="text/javascript" src="jqColorPicker.js"></script>
20+
<!-- <script type="text/javascript" src="jqColorPicker.min.js"></script> -->
21+
1522
<title>tiny jQuery color picker</title>
1623
</head>
1724
<body>
@@ -48,7 +55,8 @@ <h2>Demo</h2>
4855
<div class="trigger" value="#556B2F"><div><div></div></div></div>
4956
<div class="trigger" value="rgb(100, 86, 70)"><div><div></div></div></div>
5057
<div class="trigger" value="hsla(167, 29%, 68%, 0.8)"><div><div></div></div></div>
51-
<img id="qr" style="float: right; display: block; display: none; margin-right: -200px;" src="./development/qr.png" alt="QR code for http://www.dematte.at/tinyColorPicker/?type=mobile#demo"></div>
58+
<img id="qr" style="float: right; display: block; display: none; margin-right: -200px;" src="./development/qr.png" alt="QR code for http://www.dematte.at/tinyColorPicker/?type=mobile#demo">
59+
</div>
5260
<h2>Usage</h2>
5361
<p>There is only one file you need to load... No images and no CSS required.</p>
5462
<pre style="display: block; overflow: auto;">
@@ -77,29 +85,21 @@ <h2>Features</h2>
7785
'.cp-alpha-cursor {border-width:8px; margin-left:-8px;}',
7886

7987
renderCallback: function($elm, toggled) {
80-
var colors = this.color.colors,
81-
rgb = colors.RND.rgb;
88+
var colors = this.color.colors;
8289

8390
$('.cp-disp').css({
8491
backgroundColor: '#' + colors.HEX,
8592
color: colors.RGBLuminance > 0.22 ? '#222' : '#ddd'
86-
}).text('rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b +
87-
', ' + (Math.round(colors.alpha * 100) / 100) + ')');
93+
}).text(this.color.toString($elm._colorMode));
8894
}
8995
});
9096
</pre>
9197
<h2>API and usage</h2>
9298
<p>Will follow... See <a href="https://github.com/PitPik/tinyColorPicker">tinyColorPicker on GitHub</a> for now.</p>
9399
</div>
100+
<a class="a-inline" href="https://github.com/PitPik/tinyColorPicker"><img style="position: absolute; top: 0; right: 0; border: 0;" src="development/fmog.png" alt="Fork me on GitHub"></a>
94101

95-
96-
97-
<!-- <script type="text/javascript" src="jquery-1.11.2.js"></script> -->
98-
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
99-
<script type="text/javascript" src="colors.js"></script>
100-
<script type="text/javascript" src="jqColorPicker.js"></script>
101-
<!-- <script type="text/javascript" src="jqColorPicker.min.js"></script> -->
102102
<script type="text/javascript" src="index.js"></script>
103-
<a class="a-inline" href="https://github.com/PitPik/tinyColorPicker"><img style="position: absolute; top: 0; right: 0; border: 0;" src="development/fmog.png" alt="Fork me on GitHub"></a>
103+
104104
</body>
105105
</html>

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
'.cp-alpha-cursor{border-width: 8px; margin-left:-8px;}',
4444

4545
renderCallback: function($elm, toggled) {
46-
var colors = this.color.colors,
47-
rgb = colors.RND.rgb;
46+
var colors = this.color.colors;
4847

4948
$('.cp-disp').css({
5049
backgroundColor: '#' + colors.HEX,
5150
color: colors.RGBLuminance > 0.22 ? '#222' : '#ddd'
52-
}).text('rgba(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b +
53-
', ' + (Math.round(colors.alpha * 100) / 100) + ')');
51+
}).text(this.color.toString($elm._colorMode)); // $elm.val();
5452
}
5553
};
5654

jqColorPicker.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
}
121121

122122
function build() {
123-
$('head').append('<style type="text/css">' +
123+
$('head').append('<style type="text/css" id="tinyColorPickerStyles">' +
124124
(_options.css || _css) + (_options.cssAddon || '') + '</style>');
125125

126126
return _colorPicker.$UI = _$UI =
@@ -200,22 +200,13 @@
200200
HSL = colors.RND.hsl,
201201
dark = '#222',
202202
light = '#ddd',
203-
colorMode = _$trigger._colorMode,
204-
isAlpha = colors.alpha !== 1,
205-
alpha = _round(colors.alpha * 100) / 100,
206-
RGBInnerText = RGB.r + ', ' + RGB.g + ', ' + RGB.b,
207-
text = (colorMode === 'HEX' && !isAlpha ? '#' + colors.HEX :
208-
colorMode === 'rgb' || (colorMode === 'HEX' && isAlpha) ?
209-
(!isAlpha ? 'rgb(' + RGBInnerText + ')' :
210-
'rgba(' + RGBInnerText + ', ' + alpha + ')') :
211-
('hsl' + (isAlpha ? 'a(' : '(') + HSL.h + ', ' + HSL.s + '%, ' +
212-
HSL.l + '%' + (isAlpha ? ', ' + alpha : '') + ')')),
203+
colorText = _color.toString(_$trigger._colorMode),
213204
HUEContrast = colors.HUELuminance > 0.22 ? dark : light,
214205
alphaContrast = colors.rgbaMixBlack.luminance > 0.22 ? dark : light,
215206
h = (1 - colors.hsv.h) * _$xy_slider._height,
216207
s = colors.hsv.s * _$xy_slider._width,
217208
v = (1 - colors.hsv.v) * _$xy_slider._height,
218-
a = alpha * _$alpha._width,
209+
a = colors.alpha * _$alpha._width,
219210
translate3d = _GPU ? 'translate3d' : '',
220211
triggerValue = _$trigger[0].value,
221212
hasNoValue = _$trigger[0].hasAttribute('value') && // question this
@@ -235,18 +226,18 @@
235226
top: !_GPU ? h : '',
236227
borderColor : 'transparent ' + HUEContrast
237228
};
238-
_$alpha._css = {backgroundColor: 'rgb(' + RGBInnerText + ')'};
229+
_$alpha._css = {backgroundColor: '#' + colors.HEX};
239230
_$alpha_cursor._css = {
240231
transform: translate3d + '(' + a + 'px, 0, 0)',
241232
left: !_GPU ? a : '',
242233
borderColor : alphaContrast + ' transparent'
243234
};
244235
_$trigger._css = {
245-
backgroundColor : hasNoValue ? '' : text,
236+
backgroundColor : hasNoValue ? '' : colorText,
246237
color: hasNoValue ? '' :
247238
colors.rgbaMixBGMixCustom.luminance > 0.22 ? dark : light
248239
};
249-
_$trigger.text = hasNoValue ? '' : triggerValue !== text ? text : '';
240+
_$trigger.text = hasNoValue ? '' : triggerValue !== colorText ? colorText : '';
250241

251242
toggled !== undefined ? render(toggled) : _animate(render);
252243
}

jqColorPicker.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)