File: circles.js

package info (click to toggle)
qlcplus 4.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,644 kB
  • sloc: cpp: 182,867; javascript: 7,764; xml: 2,453; ansic: 2,120; sh: 1,716; python: 634; ruby: 606; makefile: 23
file content (278 lines) | stat: -rw-r--r-- 8,457 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
  Q Light Controller Plus
  circles.js

  Copyright (c) Massimo Callegari
  with Additions by Branson Matheson 

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

// Development tool access
var testAlgo;

(
  function()
  {
    var algo = new Object;
    algo.apiVersion = 2;
    algo.name = "Circles";
    algo.author = "Massimo Callegari+Branson Matheson";
    algo.acceptColors = 2;
    algo.properties = new Array();
    algo.circlesAmount = 3;
    algo.properties.push("name:circlesAmount|type:range|display:Amount|values:1,10|write:setAmount|read:getAmount");
    algo.circlesSize = 0;
    algo.properties.push("name:circlesSize|type:range|display:Diameter(0=max(h,v))|values:0,32|write:setSize|read:getSize");
    algo.fadeMode = 0;
    algo.properties.push("name:fadeMode|type:list|display:Fade Mode|values:Don't Fade,Fade In,Fade Out,Pulse|write:setFade|read:getFade");
    algo.fillCircles = 0;
    algo.properties.push("name:fillCircles|type:list|display:Fill circles|values:No,Yes|write:setFill|read:getFill");

    var util = new Object;
    util.pixelMap = new Array();
    util.initialized = false;
    util.circlesMaxSize = 0;

    var circles = new Array();

    function Circle(x, y, step, rgb)
    {
      this.xCenter = x;
      this.yCenter = y;
      this.step = step;
      this.rgb = rgb;
    }

    algo.setAmount = function(_amount)
    {
      algo.circlesAmount = _amount;
      util.initialized = false;
    };

    algo.getAmount = function()
    {
      return algo.circlesAmount;
    };

    algo.setSize = function(_size)
    {
      algo.circlesSize = _size;
      util.initialized = false;
    };

    algo.getSize = function()
    {
      return algo.circlesSize;
    };

    algo.setFade = function(_fade)
    {
      if (_fade === "Fade In") { algo.fadeMode = 1; }
      else if (_fade === "Fade Out") { algo.fadeMode = 2; }
      else if (_fade === "Pulse") { algo.fadeMode = 3; }
      else { algo.fadeMode = 0; }
    };

    algo.getFade = function()
    {
      if (algo.fadeMode === 1) { return "Fade In"; }
      else if (algo.fadeMode === 2) { return "Fade Out"; }
      else if (algo.fadeMode === 3) { return "Pulse"; }
      else { return "Don't Fade"; }
    };

    algo.setFill = function (_fill) {
      if (_fill === "Yes") {
        algo.fillCircles = 1;
      } else {
        algo.fillCircles = 0;
      }
    };

    algo.getFill = function () {
      if (algo.fillCircles === 1) {
        return "Yes";
      } else {
        return "No";
      }
    };

    util.initialize = function(size, rgb)
    {
      if (size > 0) {
        util.circlesMaxSize = size;
      }

      circles = new Array();
      for (var i = 0; i < algo.circlesAmount; i++) {
        circles[i] = new Circle(-1, -1, 0, rgb);
      }

      util.initialized = true;
    };

    util.getStepColor = function(step, rgb)
    {
      if (algo.fadeMode === 0)
      {
        return rgb;
      }
      else
      {
        var r = (rgb >> 16) & 0x00FF;
        var g = (rgb >> 8) & 0x00FF;
        var b = rgb & 0x00FF;

        var stepCount = Math.floor(util.circlesMaxSize / 2);
        var fadeStep = step;
        if ( algo.fadeMode === 2 ) {
          fadeStep = stepCount - step;
        } else if (algo.fadeMode == 3 && step >= stepCount/2) {
          fadeStep = stepCount - step + (stepCount/2) -1;
        } else if (algo.fadeMode == 3 && step < stepCount/2) {
          fadeStep = step + (stepCount/2) -1;
        }
        var newR = Math.round((r / stepCount) * fadeStep);
        var newG = Math.round((g / stepCount) * fadeStep);
        var newB = Math.round((b / stepCount) * fadeStep);
        var newRGB = (newR << 16) + (newG << 8) + newB;
        //console.log("mode" + fadeMode + " rgb: " + r + g + b + " nrgb: " + newR+newG+newB +" StepCount: " + stepCount + " step:" + step + " fadeStep:" + fadeStep )
        return newRGB;
      }
    };

    // from https://www.redblobgames.com/grids/circle-drawing/ 
    util.drawCircle = function(cx, cy, color, width, height, r) {
      ctop = Math.max(0, cy - r)
      cbottom = Math.min(height, cy + r )
      cleft = Math.max(0, cx - r)
      cright = Math.min(width, cx + r )
      for (py = ctop; py <= cbottom; py++) {
        for (px = cleft; px <= cright; px++ ) {
          dx = cx - px;
          dy = cy - py;
          distance_squared = (dx * dx) + (dy * dy);
          if ( distance_squared <= r*r-1 ) {  // -1 here so edges are clean
            util.drawPixel(px, py, color, width, height);
          }
        }
      }
    }

    util.drawPixel = function(cx, cy, color, width, height)
    {
      //cx = cx.toFixed(0);
      //cy = cy.toFixed(0);
      cx = Math.round(cx);
      cy = Math.round(cy);
      if (cx >= 0 && cx < width && cy >= 0 && cy < height) {
        util.pixelMap[cy][cx] = color;
      }
    };

    util.getNextStep = function(width, height, rgb)
    {
      var x = 0;
      var y = 0;
      // create an empty, black pixelMap
      util.pixelMap = new Array(height);
      for (y = 0; y < height; y++)
      {
        util.pixelMap[y] = new Array(width);
        for (x = 0; x < width; x++) {
          util.pixelMap[y][x] = 0;
        }
      }

      for (var i = 0; i < algo.circlesAmount; i++)
      {
        if (circles[i].xCenter === -1)
        {
          circles[i].rgb = rgb;
        }
        var color = util.getStepColor(circles[i].step, circles[i].rgb);
        //alert("Circle " + i + " xCenter: " + circles[i].xCenter + " color: " + color.toString(16));
        if (circles[i].xCenter === -1)
        {
          var seed = Math.floor(Math.random()*100);
          if (seed > 50) { continue; }
          circles[i].xCenter = Math.floor(Math.random() * width);
          circles[i].yCenter = Math.floor(Math.random() * height);
          util.pixelMap[circles[i].yCenter][circles[i].xCenter] = color;
        }
        else
        {
          var l = circles[i].step * Math.cos(Math.PI / 4);
          var radius2 = circles[i].step * circles[i].step;
          l = l.toFixed(0);

          if ( algo.fillCircles == 0 ) {
            for (x = 0; x <= l; x++)
            {
              y = Math.sqrt(radius2 - (x * x));

              util.drawPixel(circles[i].xCenter + x, circles[i].yCenter + y, color, width, height);
              util.drawPixel(circles[i].xCenter + x, circles[i].yCenter - y, color, width, height);
              util.drawPixel(circles[i].xCenter - x, circles[i].yCenter + y, color, width, height);
              util.drawPixel(circles[i].xCenter - x, circles[i].yCenter - y, color, width, height);

              util.drawPixel(circles[i].xCenter + y, circles[i].yCenter + x, color, width, height);
              util.drawPixel(circles[i].xCenter + y, circles[i].yCenter - x, color, width, height);
              util.drawPixel(circles[i].xCenter - y, circles[i].yCenter + x, color, width, height);
              util.drawPixel(circles[i].xCenter - y, circles[i].yCenter - x, color, width, height);
            }
          } else {
            util.drawCircle(circles[i].xCenter, circles[i].yCenter, color, width, height, circles[i].step)
          }
        } 

        circles[i].step++;
        if (circles[i].step >= (util.circlesMaxSize / 2))
        {
          circles[i].xCenter = -1;
          circles[i].yCenter = -1;
          circles[i].step = 0;
        }
      }

      return util.pixelMap;
    };

    algo.rgbMap = function(width, height, rgb, step)
    {
      if (util.initialized === false)
      {
        if ( algo.circlesSize > 0 ) {
          util.initialize(algo.circlesSize, rgb);
        } else if (height < width) {
          util.initialize(height, rgb);
        } else {
          util.initialize(width, rgb);
        }
      }

      return util.getNextStep(width, height, rgb);
    };

    algo.rgbMapStepCount = function(width, height)
    {
      return 2;
    };

    // Development tool access
    testAlgo = algo;

    return algo;
    }
)();