1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
10
* The copyright notice above does not evidence any actual of intended
11
* publication of such source code, and is an unpublished work by create3000.
12
* This material contains CONFIDENTIAL INFORMATION that is the property of
15
* No permission is granted to copy, distribute, or create derivative works from
16
* the contents of this software, in whole or in part, without the prior written
17
* permission of create3000.
19
* NON-MILITARY USE ONLY
21
* All create3000 software are effectively free software with a non-military use
22
* restriction. It is free. Well commented source is provided. You may reuse the
23
* source in any way you please with the exception anything that uses it must be
24
* marked to indicate is contains 'non-military use only' components.
26
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
28
* Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
30
* This file is part of the Cobweb Project.
32
* Cobweb is free software: you can redistribute it and/or modify it under the
33
* terms of the GNU General Public License version 3 only, as published by the
34
* Free Software Foundation.
36
* Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
37
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
38
* A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
39
* details (a copy is included in the LICENSE file that accompanied this code).
41
* You should have received a copy of the GNU General Public License version 3
42
* along with Cobweb. If not, see <http://www.gnu.org/licenses/gpl.html> for a
43
* copy of the GPLv3 License.
45
* For Silvio, Joy and Adi.
47
******************************************************************************/
52
"standard/Math/Algorithm",
54
function ($, Algorithm)
58
var clamp = Algorithm .clamp;
60
function Color3 (r, g, b)
62
if (arguments .length)
64
this .r_ = clamp (r, 0, 1);
65
this .g_ = clamp (g, 0, 1);
66
this .b_ = clamp (b, 0, 1);
82
var copy = Object .create (Color3 .prototype);
88
assign: function (color)
94
set: function (r, g, b)
96
this .r_ = clamp (r, 0, 1);
97
this .g_ = clamp (g, 0, 1);
98
this .b_ = clamp (b, 0, 1);
100
equals: function (color)
102
return this .r_ === color .r_ &&
103
this .g_ === color .g_ &&
104
this .b_ === color .b_;
106
getHSV: function (result)
110
var min = Math .min (this .r_, this .g_, this .b_);
111
var max = Math .max (this .r_, this .g_, this .b_);
114
var delta = max - min;
116
if (max !== 0 && delta !== 0)
118
s = delta / max; // s
120
if (this .r_ === max)
121
h = (this .g_ - this .b_) / delta; // between yellow & magenta
122
else if (this .g_ === max)
123
h = 2 + (this .b_ - this .r_) / delta; // between cyan & yellow
125
h = 4 + (this .r_ - this .g_) / delta; // between magenta & cyan
127
h *= Math .PI / 3; // radiants
132
s = h = 0; // s = 0, h is undefined
140
setHSV: function (h, s, v)
145
// H is given on [0, 2 * Pi]. S and V are given on [0, 1].
146
// RGB are each returned on [0, 1].
151
this .r_ = this .g_ = this .b_ = v;
155
var w = Algorithm .degrees (Algorithm .interval (h, 0, Math .PI * 2)) / 60; // sector 0 to 5
157
var i = Math .floor (w);
158
var f = w - i; // factorial part of h
159
var p = v * ( 1 - s );
160
var q = v * ( 1 - s * f );
161
var t = v * ( 1 - s * ( 1 - f ) );
165
case 0: this .r_ = v; this .g_ = t; this .b_ = p; break;
166
case 1: this .r_ = q; this .g_ = v; this .b_ = p; break;
167
case 2: this .r_ = p; this .g_ = v; this .b_ = t; break;
168
case 3: this .r_ = p; this .g_ = q; this .b_ = v; break;
169
case 4: this .r_ = t; this .g_ = p; this .b_ = v; break;
170
default: this .r_ = v; this .g_ = p; this .b_ = q; break;
174
toString: function ()
176
return this .r_ + " " +
183
get: function () { return this .r_; },
184
set: function (value) { this .r_ = clamp (value, 0, 1); },
190
get: function () { return this .g_; },
191
set: function (value) { this .g_ = clamp (value, 0, 1); },
197
get: function () { return this .b_; },
198
set: function (value) { this .b_ = clamp (value, 0, 1); },
203
Object .defineProperty (Color3 .prototype, "r", r);
204
Object .defineProperty (Color3 .prototype, "g", g);
205
Object .defineProperty (Color3 .prototype, "b", b);
207
r .enumerable = false;
208
g .enumerable = false;
209
b .enumerable = false;
211
Object .defineProperty (Color3 .prototype, "0", r);
212
Object .defineProperty (Color3 .prototype, "1", g);
213
Object .defineProperty (Color3 .prototype, "2", b);
217
HSV: function (h, s, v)
219
var color = new Color3 (0, 0, 0);
220
color .setHSV (h, s, v);
223
lerp: function (a, b, t, r)
225
var range = Math .abs (b [0] - a [0]);
227
if (range <= Math .PI)
229
r [0] = Algorithm .lerp (a [0], b [0], t);
230
r [1] = Algorithm .lerp (a [1], b [1], t);
231
r [2] = Algorithm .lerp (a [2], b [2], t);
237
step = (PI2 - range) * t,
238
h = a [0] < b [0] ? a [0] - step : a [0] + step;
247
r [1] = Algorithm .lerp (a [1], b [1], t);
248
r [2] = Algorithm .lerp (a [2], b [2], t);