Skip to content

Commit 613290a

Browse files
committed
update docs and move complement into combination function. Issue #56
1 parent c1b7f7c commit 613290a

File tree

3 files changed

+127
-31
lines changed

3 files changed

+127
-31
lines changed

README.md

Lines changed: 112 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# TinyColor
22

3-
## JavaScript color parsing
3+
## JavaScript color tooling
44

5-
Fast, small color manipulation and conversion for JavaScript. TinyColor is allows many forms of input, while providing color conversions and other color utility functions. It has no dependencies.
5+
TinyColor is a small, fast library for color manipulation and conversion in JavaScript. It allows many forms of input, while providing color conversions and other color utility functions. It has no dependencies.
66

77
[![Build Status](https://travis-ci.org/bgrins/TinyColor.png?branch=master)](https://travis-ci.org/bgrins/TinyColor)
88

99
## Including in a browser
1010

11+
Download [tinycolor.js](https://raw.githubusercontent.com/bgrins/TinyColor/master/tinycolor.js) or install it with bower:
12+
13+
bower install tinycolor
14+
15+
Then just include it in the page in a `script` tag:
16+
1117
<script type='text/javascript' src='tinycolor.js'></script>
1218
<script type='text/javascript'>
1319
var color = tinycolor("red");
@@ -238,33 +244,118 @@ Print to a string, depending on the input format. You can also override this by
238244
color2.setAlpha(.5);
239245
color2.toString(); // "rgba(255, 0, 0, 0.5)"
240246

241-
## Color Utilities
247+
### Color Modification
242248

243-
tinycolor.equals(color1, color2)
249+
These methods manipulate the current color, and return it for chaining. For instance:
244250

245-
### Color Modification
251+
tinycolor("red").lighten().desaturate().toHexString() // "#f53d3d"
246252

247-
Modification functions may take an `amount` variable from 0 - 100, indicating how much the effect should be applied.
253+
### lighten
248254

249-
tinycolor.lighten(color, amount = 10)
250-
tinycolor.brighten(color, amount = 10)
251-
tinycolor.darken(color, amount = 10)
252-
tinycolor.desaturate(color, amount = 10)
253-
tinycolor.saturate(color, amount = 10)
254-
tinycolor.greyscale(color)
255-
tinycolor.spin(color, amount)
256-
tinycolor.mix(color1, color2, amount = 50)
255+
`lighten: function(amount = 10) -> TinyColor`. Lighten the color a given amount, from 0 to 100. Providing 100 will always return white.
256+
257+
tinycolor("#f00").lighten().toString(); // "#ff3333"
258+
tinycolor("#f00").lighten(100).toString(); // "#ffffff"
259+
260+
### brighten
261+
262+
`brighten: function(amount = 10) -> TinyColor`. Brighten the color a given amount, from 0 to 100.
263+
264+
tinycolor("#f00").brighten().toString(); // "#ff1919"
265+
266+
### darken
267+
268+
`darken: function(amount = 10) -> TinyColor`. Darken the color a given amount, from 0 to 100. Providing 100 will always return black.
269+
270+
tinycolor("#f00").darken().toString(); // "#cc0000"
271+
tinycolor("#f00").darken(100).toString(); // "#000000"
272+
273+
### desaturate
274+
275+
`desaturate: function(amount = 10) -> TinyColor`. Desaturate the color a given amount, from 0 to 100. Providing 100 will is the same as calling `greyscale`.
276+
277+
tinycolor("#f00").desaturate().toString(); // "#f20d0d"
278+
tinycolor("#f00").desaturate(100).toString(); // "#808080"
279+
280+
### saturate
281+
282+
`saturate: function(amount = 10) -> TinyColor`. Saturate the color a given amount, from 0 to 100.
283+
284+
tinycolor("hsl(0, 10%, 50%)").saturate().toString(); // "hsl(0, 20%, 50%)"
285+
286+
### greyscale
287+
288+
`greyscale: function() -> TinyColor`. Completely desaturates a color into greyscale. Same as calling `desaturate(100)`.
289+
290+
tinycolor("#f00").greyscale().toString(); // "#808080"
291+
292+
### spin
293+
294+
`spin: function(amount = 0) -> TinyColor`. Spin the hue a given amount, from -360 to 360. Calling with 0, 360, or -360 will do nothing (since it sets the hue back to what it was before).
295+
296+
tinycolor("#f00").spin(180).toString(); // "#00ffff"
297+
tinycolor("#f00").spin(-90).toString(); // "#7f00ff"
298+
tinycolor("#f00").spin(90).toString(); // "#80ff00"
299+
300+
// spin(0) and spin(360) do nothing
301+
tinycolor("#f00").spin(0).toString(); // "#ff0000"
302+
tinycolor("#f00").spin(360).toString(); // "#ff0000"
257303

258304
### Color Combinations
259305

260-
Combination functions return an Array of TinyColor objects.
306+
Combination functions return an array of TinyColor objects unless otherwise noted.
261307

262-
tinycolor.analogous(color, results = 6, slices = 30)
263-
tinycolor.complement(color)
264-
tinycolor.monochromatic(color, results = 6)
265-
tinycolor.splitcomplement(color)
266-
tinycolor.triad(color)
267-
tinycolor.tetrad(color)
308+
### analogous
309+
310+
`analogous: function(, results = 6, slices = 30) -> array<TinyColor>`.
311+
312+
var colors = tinycolor("#f00").analogous();
313+
314+
colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#ff0066", "#ff0033", "#ff0000", "#ff3300", "#ff6600" ]
315+
316+
### monochromatic
317+
318+
`monochromatic: function(, results = 6) -> array<TinyColor>`.
319+
320+
var colors = tinycolor("#f00").monochromatic();
321+
322+
colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#2a0000", "#550000", "#800000", "#aa0000", "#d40000" ]
323+
324+
### splitcomplement
325+
326+
`splitcomplement: function() -> array<TinyColor>`.
327+
328+
var colors = tinycolor("#f00").splitcomplement();
329+
330+
colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#ccff00", "#0066ff" ]
331+
332+
### triad
333+
334+
`triad: function() -> array<TinyColor>`.
335+
336+
var colors = tinycolor("#f00").triad();
337+
338+
colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#00ff00", "#0000ff" ]
339+
340+
### tetrad
341+
342+
`tetrad: function() -> array<TinyColor>`.
343+
344+
var colors = tinycolor("#f00").tetrad();
345+
346+
colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]
347+
348+
349+
### complement
350+
351+
`complement: function() -> TinyColor`.
352+
353+
tinycolor("#f00").complement().toHexString(); // "#00ffff"
354+
355+
## Color Utilities
356+
357+
tinycolor.equals(color1, color2)
358+
tinycolor.mix(color1, color2, amount = 50)
268359

269360
### Readability
270361

test/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,6 @@ test("Modifications", function () {
577577
}
578578

579579
equal (tinycolor("red").greyscale().toHex(), "808080", "Greyscale works");
580-
equal (tinycolor("red").complement().toHex(), "00ffff", "Complement works");
581580
});
582581

583582
test("Spin", function () {
@@ -625,6 +624,12 @@ function colorsToHexString(colors) {
625624
}).join(",");
626625
}
627626

627+
test("complement", function() {
628+
var complementDoesntModifyInstance = tinycolor("red");
629+
equal (complementDoesntModifyInstance.complement().toHex(), "00ffff", "Complement works");
630+
equal (complementDoesntModifyInstance.toHex(), "ff0000", "Complement did not modify this color");
631+
});
632+
628633
test("analogous", function() {
629634
var combination = tinycolor("red").analogous();
630635
equal(colorsToHexString(combination), "ff0000,ff0066,ff0033,ff0000,ff3300,ff6600", "Correct Combination");

tinycolor.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,16 @@ tinycolor.prototype = {
219219
spin: function() {
220220
return this._applyModification(spin, arguments);
221221
},
222-
complement: function() {
223-
return this._applyModification(complement, arguments);
224-
},
225222

226223
_applyCombination: function(fn, args) {
227224
return fn.apply(null, [this].concat([].slice.call(args)));
228225
},
229226
analogous: function() {
230227
return this._applyCombination(analogous, arguments);
231228
},
229+
complement: function() {
230+
return this._applyCombination(complement, arguments);
231+
},
232232
monochromatic: function() {
233233
return this._applyCombination(monochromatic, arguments);
234234
},
@@ -566,12 +566,6 @@ function darken (color, amount) {
566566
return tinycolor(hsl);
567567
}
568568

569-
function complement(color) {
570-
var hsl = tinycolor(color).toHsl();
571-
hsl.h = (hsl.h + 180) % 360;
572-
return tinycolor(hsl);
573-
}
574-
575569
// Spin takes a positive or negative amount within [-360, 360] indicating the change of hue.
576570
// Values outside of this range will be wrapped into this range.
577571
function spin(color, amount) {
@@ -586,6 +580,12 @@ function spin(color, amount) {
586580
// Thanks to jQuery xColor for some of the ideas behind these
587581
// <https://github.com/infusion/jQuery-xcolor/blob/master/jquery.xcolor.js>
588582

583+
function complement(color) {
584+
var hsl = tinycolor(color).toHsl();
585+
hsl.h = (hsl.h + 180) % 360;
586+
return tinycolor(hsl);
587+
}
588+
589589
function triad(color) {
590590
var hsl = tinycolor(color).toHsl();
591591
var h = hsl.h;

0 commit comments

Comments
 (0)