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
|
/*
Q Light Controller Plus
blinder.js
Copyright (c) Hans-Jürgen Tappe
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 = "Blinder";
algo.author = "Hans-Jürgen Tappe";
algo.acceptColors = 2;
algo.properties = new Array();
algo.divisor = 1;
algo.properties.push("name:divisor|type:range|display:Divisor|values:1,30|write:setDivisor|read:getDivisor");
// Prepare internal properties
algo.size = 0;
algo.width = 0;
algo.height = 0;
algo.numX = 0;
algo.numY = 0;
var util = new Object;
algo.initialized = false;
algo.setDivisor = function(_divisor)
{
algo.divisor = _divisor;
algo.initialized = false;
};
algo.getDivisor = function()
{
return algo.divisor;
};
// Combine RGB color from color channels
function mergeRgb(r, g, b) {
r = Math.min(255, Math.round(r));
g = Math.min(255, Math.round(g));
b = Math.min(255, Math.round(b));
return ((r << 16) + (g << 8) + b);
}
function getColor(r, g, b, mRgb) {
// split rgb in to components
var pointr = (mRgb >> 16) & 0x00FF;
var pointg = (mRgb >> 8) & 0x00FF;
var pointb = mRgb & 0x00FF;
// add the color to the mapped location
pointr += r;
pointg += g;
pointb += b;
// set mapped point
return mergeRgb(pointr, pointg, pointb);
}
util.initialize = function(width, height)
{
algo.width = width;
algo.height = height;
if (width <= height) {
algo.size = width / algo.divisor;
algo.numX = algo.divisor;
algo.numY = Math.floor(height / algo.size);
} else {
algo.size = height / algo.divisor;
algo.numX = Math.floor(width / algo.size);
algo.numY = algo.divisor;
}
algo.bulb = new Array();
var count = 0;
for (var w = 0; w < algo.numX; w++) {
for (var h = 0; h < algo.numY; h++) {
algo.bulb[count] = {
// width / algo.numX * w + width / algo.numX / 2
x: (2 * w + 1 ) * width / 2 / algo.numX - 0.5,
y: (2 * h + 1 ) * height / 2 / algo.numY - 0.5,
};
algo.bulb[count].xMin = Math.floor(algo.bulb[count].x - algo.size / 2) - 1;
algo.bulb[count].xMax = Math.ceil(algo.bulb[count].x + algo.size / 2) - 1;
algo.bulb[count].yMin = Math.floor(algo.bulb[count].y - algo.size / 2) - 1;
algo.bulb[count].yMax = Math.ceil(algo.bulb[count].y + algo.size / 2) - 1;
count ++;
}
}
algo.initialized = true;
return;
};
algo.rgbMap = function(width, height, rgb, progstep)
{
if (algo.initialized === false || width !== algo.width || height !== algo.height) {
util.initialize(width, height);
}
// Clear map data
var map = new Array(height);
for (var y = 0; y < height; y++) {
map[y] = new Array();
for (var x = 0; x < width; x++) {
map[y][x] = 0;
}
}
var r = (rgb >> 16) & 0x00FF;
var g = (rgb >> 8) & 0x00FF;
var b = rgb & 0x00FF;
var stepPercent = progstep / (algo.rgbMapStepCount(width, height) - 1);
var bgPower = Math.pow(stepPercent + 0.1, 2);
var bgFactor = Math.min(1, bgPower);
// Calculate color pixel related to their positions
var bgPointr = r * bgFactor;
var bgPointg = g * bgFactor;
var bgPointb = b * bgFactor;
// for each bulb displayed
for (var i = 0; i < algo.bulb.length; i++) {
for (var ry = algo.bulb[i].yMin; ry <= algo.bulb[i].yMax; ry++) {
for (var rx = algo.bulb[i].xMin; rx <= algo.bulb[i].xMax; rx++) {
// Draw only if edges are on the map
if (rx >= 0 && rx < width && ry >= 0 && ry < height) {
// Draw the box for debugging.
//map[ry][rx] = getColor(40, 40, 40, map[ry][rx]);
// calculate the offset difference of map location to the float
// location of the tree
var offx = Math.abs((rx - algo.bulb[i].x) / (algo.size / 2));
var offy = Math.abs((ry - algo.bulb[i].y) / (algo.size / 2));
// Try at: www.geogebra.org/3d
// f(x,y) = 4 sin(π/2 * sqrt(1 - x^2 - y^2))
// + 6 cos(π/2 * sqrt(1 - x^2 - y^2))
// - sqrt((4 * x)^2 + (4 * y)^2)
// - 3
// + relative step
var s = 1 - offx * offx - offy * offy;
if (s > 0) {
var aFactor = 4 * Math.sin(Math.PI / 2 * Math.sqrt(1 - offx * offx - offy * offy));
var bFactor = 6 * Math.cos(Math.PI / 2 * Math.sqrt(1 - offx * offx - offy * offy));
var cFactor = Math.sqrt(4 * offx * 4 * offx + 4 * offy * 4 * offy);
var factor = aFactor
+ bFactor
- cFactor
- 3
+ stepPercent;
if (factor < 0) {
factor = 0;
}
// Calculate color pixel related to their positions
var pointr = r * factor;
var pointg = g * factor;
var pointb = b * factor;
// add the bulb color to the mapped location
map[ry][rx] = getColor(pointr, pointg, pointb, map[ry][rx]);
}
}
}
}
}
// Apply the backgruond color.
for (var ry = 0; ry < height; ry++) {
for (var rx = 0; rx < width; rx++) {
r = Math.max(bgPointr, (map[ry][rx] >> 16) & 0x00FF);
g = Math.max(bgPointg, (map[ry][rx] >> 8) & 0x00FF);
b = Math.max(bgPointb, map[ry][rx] & 0x00FF);
map[ry][rx] = getColor(r, g, b, 0);
}
}
return map;
};
algo.rgbMapStepCount = function(width, height)
{
return 32;
};
// Development tool access
testAlgo = algo;
return algo;
}
)();
|