Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

much better angle-based algorithm

  • Loading branch information...
commit 1fa17eecb20cc5fc22111ae34ad5b2c9696baa39 1 parent dfb06c1
James Halliday authored

Showing 2 changed files with 20 additions and 32 deletions. Show diff stats Hide diff stats

  1. +2 2 example/browser/main.js
  2. +18 30 index.js
4 example/browser/main.js
@@ -2,11 +2,11 @@ var mr = require('mrcolor');
2 2 var $ = require('jquery-browserify');
3 3
4 4 $(window).ready(function () {
5   - var color = mr();
  5 + var next = mr();
6 6
7 7 $('#clicky').click(function () {
  8 + var color = next();
8 9 var rgb = 'rgb(' + color.rgb().join(',') + ')';
9   - color = color.next();
10 10
11 11 $('<div>')
12 12 .css({
48 index.js
... ... @@ -1,41 +1,31 @@
1 1 var convert = require('color-convert');
2 2
3 3 var mr = module.exports = function () {
4   - var ranges = [];
5   - var colors = [];
  4 + var angle = 0;
  5 + var step = 60;
6 6
7   - function nextTwo (min, max) {
8   - var dx = (max - min) / 2;
9   -
10   - var i = min;
11   - var j = Math.floor(dx + min);
12   -
13   - colors.push(mr.fromIndex(i));
14   - colors.push(mr.fromIndex(j));
15   -
16   - ranges.push([ Math.floor(i + dx / 2), j ]);
17   - ranges.push([ Math.floor(j + dx / 2), max ]);
18   - }
  7 + var used = {};
19 8
20   - return (function next () {
21   - if (colors.length) {
22   - var c = colors.shift();
23   - c.next = next;
24   - return c;
  9 + return function next () {
  10 + if (angle + step > 360) {
  11 + step /= 2;
  12 + angle = step;
25 13 }
26   - else if (ranges.length) {
27   - var r = ranges.shift();
28   - nextTwo.apply(null, r);
  14 +
  15 + var a = Math.floor(angle);
  16 + angle += step;
  17 +
  18 + if (used[a]) {
29 19 return next();
30 20 }
31 21 else {
32   - nextTwo(0, 360);
33   - return next();
  22 + used[a] = true;
  23 + return mr.fromAngle(a);
34 24 }
35   - })();
  25 + };
36 26 };
37 27
38   -mr.fromIndex = function (i) {
  28 +mr.fromAngle = function (i) {
39 29 return {
40 30 index : i,
41 31 rgb : function () {
@@ -60,12 +50,10 @@ mr.take = function (n) {
60 50 if (n <= 0) return [];
61 51
62 52 var res = [];
63   - var c = { next : mr };
  53 + var next = mr();
64 54
65 55 for (var i = 0; i < n; i++) {
66   - var c_ = c;
67   - c = c.next();
68   - res.push(c);
  56 + res.push(next());
69 57 }
70 58
71 59 return res;

0 comments on commit 1fa17ee

Please sign in to comment.
Something went wrong with that request. Please try again.