Skip to content
Snippets Groups Projects
Commit d5b99f53 authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

HSV color for opc.js

parent 940e7b13
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,11 @@
var net = require('net');
var fs = require('fs');
/********************************************************************************
* Core OPC Client
*/
var OPC = function(host, port)
{
this.host = host;
......@@ -15,12 +20,6 @@ var OPC = function(host, port)
this.pixelBuffer = null;
};
OPC.loadModel = function(filename)
{
// Synchronously load a JSON model from a file on disk
return JSON.parse(fs.readFileSync(filename))
}
OPC.prototype._reconnect = function()
{
var _this = this;
......@@ -105,6 +104,11 @@ OPC.prototype.mapPixels = function(fn, model)
this.writePixels();
}
/********************************************************************************
* Client convenience methods
*/
OPC.prototype.mapParticles = function(particles, model)
{
// Set all pixels, by mapping a particle system to each element of "model".
......@@ -140,4 +144,44 @@ OPC.prototype.mapParticles = function(particles, model)
this.mapPixels(shader, model);
}
/********************************************************************************
* Global convenience methods
*/
OPC.loadModel = function(filename)
{
// Synchronously load a JSON model from a file on disk
return JSON.parse(fs.readFileSync(filename))
}
OPC.hsv = function(h, s, v)
{
/*
* Converts an HSV color value to RGB.
*
* Normal hsv range is in [0, 1], RGB range is [0, 255].
* Colors may extend outside these bounds. Hue values will wrap.
*
* Based on tinycolor:
* https://github.com/bgrins/TinyColor/blob/master/tinycolor.js
* 2013-08-10, Brian Grinstead, MIT License
*/
h *= 6;
var i = h | 0,
f = h - i,
p = v * (1 - s),
q = v * (1 - f * s),
t = v * (1 - (1 - f) * s),
mod = i % 6,
r = [v, q, p, p, t, v][mod],
g = [t, v, v, q, p, p][mod],
b = [p, p, t, v, v, q][mod];
return [ r * 255, g * 255, b * 255 ];
}
module.exports = OPC;
......@@ -9,23 +9,25 @@ var client = new OPC('localhost', 7890);
function draw() {
var t = 0.009 * new Date().getTime();
var time = 0.009 * new Date().getTime();
var numParticles = 200;
var particles = [];
for (var i = 0; i < numParticles; i++) {
var s = i / numParticles;
var r = 0.2 + 1.5 * i / numParticles;
var x = r * Math.cos(t);
var y = r * Math.sin(t + 10.0 * Math.sin(t * 0.15));
var radius = 0.2 + 1.5 * s;
var theta = time + 0.04 * i;
var x = radius * Math.cos(theta);
var y = radius * Math.sin(theta + 10.0 * Math.sin(theta * 0.15));
var hue = time * 0.01 + s * 0.2;
particles[i] = {
point: [0, x, y],
intensity: 40 * i / numParticles,
intensity: 0.2 * s,
falloff: 60,
color: [0.9, 0.2, i * 0.01]
color: OPC.hsv(hue, 0.5, 0.8)
};
t += 0.04;
}
client.mapParticles(particles, model);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment