~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to cobweb.js/standard/Math/Numbers/Color3.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
 
 *******************************************************************************
3
 
 *
4
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 
 *
6
 
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
7
 
 *
8
 
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
9
 
 *
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
13
 
 * create3000.
14
 
 *
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.
18
 
 *
19
 
 * NON-MILITARY USE ONLY
20
 
 *
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.
25
 
 *
26
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27
 
 *
28
 
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
29
 
 *
30
 
 * This file is part of the Cobweb Project.
31
 
 *
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.
35
 
 *
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).
40
 
 *
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.
44
 
 *
45
 
 * For Silvio, Joy and Adi.
46
 
 *
47
 
 ******************************************************************************/
48
 
 
49
 
 
50
 
define ([
51
 
        "jquery",
52
 
        "standard/Math/Algorithm",
53
 
],
54
 
function ($, Algorithm)
55
 
{
56
 
"use strict";
57
 
 
58
 
        var clamp = Algorithm .clamp;
59
 
 
60
 
        function Color3 (r, g, b)
61
 
        {
62
 
                if (arguments .length)
63
 
                {
64
 
                        this .r_ = clamp (r, 0, 1);
65
 
                        this .g_ = clamp (g, 0, 1);
66
 
                        this .b_ = clamp (b, 0, 1);
67
 
                }
68
 
                else
69
 
                {
70
 
                        this .r_ = 0;
71
 
                        this .g_ = 0;
72
 
                        this .b_ = 0;
73
 
                }
74
 
        }
75
 
 
76
 
        Color3 .prototype =
77
 
        {
78
 
                constructor: Color3,
79
 
                length: 3,
80
 
                copy: function ()
81
 
                {
82
 
                        var copy = Object .create (Color3 .prototype);
83
 
                        copy .r_ = this .r_;
84
 
                        copy .g_ = this .g_;
85
 
                        copy .b_ = this .b_;
86
 
                        return copy;
87
 
                },
88
 
                assign: function (color)
89
 
                {
90
 
                        this .r_ = color .r_;
91
 
                        this .g_ = color .g_;
92
 
                        this .b_ = color .b_;
93
 
                },
94
 
                set: function (r, g, b)
95
 
                {
96
 
                        this .r_ = clamp (r, 0, 1);
97
 
                        this .g_ = clamp (g, 0, 1);
98
 
                        this .b_ = clamp (b, 0, 1);
99
 
                },
100
 
                equals: function (color)
101
 
                {
102
 
                        return this .r_ === color .r_ &&
103
 
                               this .g_ === color .g_ &&
104
 
                               this .b_ === color .b_;
105
 
                },
106
 
                getHSV: function (result)
107
 
                {
108
 
                        var h, s, v;
109
 
 
110
 
                        var min = Math .min (this .r_, this .g_, this .b_);
111
 
                        var max = Math .max (this .r_, this .g_, this .b_);
112
 
                        v = max; // value
113
 
 
114
 
                        var delta = max - min;
115
 
 
116
 
                        if (max !== 0 && delta !== 0)
117
 
                        {
118
 
                                s = delta / max; // s
119
 
 
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
124
 
                                else
125
 
                                        h = 4 + (this .r_ - this .g_) / delta;  // between magenta & cyan
126
 
 
127
 
                                h *= Math .PI / 3;  // radiants
128
 
                                if (h < 0)
129
 
                                        h += Math .PI * 2;
130
 
                        }
131
 
                        else
132
 
                                s = h = 0;         // s = 0, h is undefined
133
 
 
134
 
                        result [0] = h;
135
 
                        result [1] = s;
136
 
                        result [2] = v;
137
 
 
138
 
                        return result;
139
 
                },
140
 
                setHSV: function (h, s, v)
141
 
                {
142
 
                        s = clamp (s, 0, 1),
143
 
                        v = clamp (v, 0, 1);
144
 
 
145
 
                        // H is given on [0, 2 * Pi]. S and V are given on [0, 1].
146
 
                        // RGB are each returned on [0, 1].
147
 
 
148
 
                        if (s === 0)
149
 
                        {
150
 
                                // achromatic (grey)
151
 
                                this .r_ = this .g_ = this .b_ = v;
152
 
                        }
153
 
                        else
154
 
                        {
155
 
                                var w = Algorithm .degrees (Algorithm .interval (h, 0, Math .PI * 2)) / 60;     // sector 0 to 5
156
 
 
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 ) );
162
 
 
163
 
                                switch (i % 6)
164
 
                                {
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;
171
 
                                }
172
 
                        }
173
 
                },
174
 
                toString: function ()
175
 
                {
176
 
                        return this .r_ + " " +
177
 
                               this .g_ + " " +
178
 
                               this .b_;
179
 
                },
180
 
        };
181
 
 
182
 
        var r = {
183
 
                get: function () { return this .r_; },
184
 
                set: function (value) { this .r_ = clamp (value, 0, 1); },
185
 
                enumerable: true,
186
 
                configurable: false
187
 
        };
188
 
        
189
 
        var g = {
190
 
                get: function () { return this .g_; },
191
 
                set: function (value) { this .g_ = clamp (value, 0, 1); },
192
 
                enumerable: true,
193
 
                configurable: false
194
 
        };
195
 
 
196
 
        var b = {
197
 
                get: function () { return this .b_; },
198
 
                set: function (value) { this .b_ = clamp (value, 0, 1); },
199
 
                enumerable: true,
200
 
                configurable: false
201
 
        };
202
 
 
203
 
        Object .defineProperty (Color3 .prototype, "r", r);
204
 
        Object .defineProperty (Color3 .prototype, "g", g);
205
 
        Object .defineProperty (Color3 .prototype, "b", b);
206
 
 
207
 
        r .enumerable = false;
208
 
        g .enumerable = false;
209
 
        b .enumerable = false;
210
 
 
211
 
        Object .defineProperty (Color3 .prototype, "0", r);
212
 
        Object .defineProperty (Color3 .prototype, "1", g);
213
 
        Object .defineProperty (Color3 .prototype, "2", b);
214
 
 
215
 
        $.extend (Color3,
216
 
        {
217
 
                HSV: function (h, s, v)
218
 
                {
219
 
                        var color = new Color3 (0, 0, 0);
220
 
                        color .setHSV (h, s, v);
221
 
                        return color;
222
 
                },
223
 
                lerp: function (a, b, t, r)
224
 
                {
225
 
                        var range = Math .abs (b [0] - a [0]);
226
 
 
227
 
                        if (range <= Math .PI)
228
 
                        {
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);
232
 
                                return r;
233
 
                        }
234
 
 
235
 
                        var
236
 
                                PI2  = Math .PI * 2,
237
 
                                step = (PI2 - range) * t,
238
 
                                h    = a [0] < b [0] ? a [0] - step : a [0] + step;
239
 
 
240
 
                        if (h < 0)
241
 
                                h += PI2;
242
 
 
243
 
                        else if (h > PI2)
244
 
                                h -= PI2;
245
 
 
246
 
                        r [0] = h;
247
 
                        r [1] = Algorithm .lerp (a [1], b [1], t);
248
 
                        r [2] = Algorithm .lerp (a [2], b [2], t);
249
 
                        return r;
250
 
                },
251
 
        });
252
 
 
253
 
        return Color3;
254
 
});