-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathmath.ts
7569 lines (6842 loc) · 281 KB
/
math.ts
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { DeepImmutable, Nullable, FloatArray, float } from "../types";
import { ArrayTools } from "../Misc/arrayTools";
import { Scalar } from "./math.scalar";
/**
* Constant used to convert a value to gamma space
* @ignorenaming
*/
export const ToGammaSpace = 1 / 2.2;
/**
* Constant used to convert a value to linear space
* @ignorenaming
*/
export const ToLinearSpace = 2.2;
/**
* Constant used to define the minimal number value in Babylon.js
* @ignorenaming
*/
let Epsilon = 0.001;
export { Epsilon };
/**
* Class used to hold a RBG color
*/
export class Color3 {
/**
* Creates a new Color3 object from red, green, blue values, all between 0 and 1
* @param r defines the red component (between 0 and 1, default is 0)
* @param g defines the green component (between 0 and 1, default is 0)
* @param b defines the blue component (between 0 and 1, default is 0)
*/
constructor(
/**
* Defines the red component (between 0 and 1, default is 0)
*/
public r: number = 0,
/**
* Defines the green component (between 0 and 1, default is 0)
*/
public g: number = 0,
/**
* Defines the blue component (between 0 and 1, default is 0)
*/
public b: number = 0) {
}
/**
* Creates a string with the Color3 current values
* @returns the string representation of the Color3 object
*/
public toString(): string {
return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
}
/**
* Returns the string "Color3"
* @returns "Color3"
*/
public getClassName(): string {
return "Color3";
}
/**
* Compute the Color3 hash code
* @returns an unique number that can be used to hash Color3 objects
*/
public getHashCode(): number {
let hash = (this.r * 255) || 0;
hash = (hash * 397) ^ ((this.g * 255) || 0);
hash = (hash * 397) ^ ((this.b * 255) || 0);
return hash;
}
// Operators
/**
* Stores in the given array from the given starting index the red, green, blue values as successive elements
* @param array defines the array where to store the r,g,b components
* @param index defines an optional index in the target array to define where to start storing values
* @returns the current Color3 object
*/
public toArray(array: FloatArray, index: number = 0): Color3 {
array[index] = this.r;
array[index + 1] = this.g;
array[index + 2] = this.b;
return this;
}
/**
* Returns a new Color4 object from the current Color3 and the given alpha
* @param alpha defines the alpha component on the new Color4 object (default is 1)
* @returns a new Color4 object
*/
public toColor4(alpha: number = 1): Color4 {
return new Color4(this.r, this.g, this.b, alpha);
}
/**
* Returns a new array populated with 3 numeric elements : red, green and blue values
* @returns the new array
*/
public asArray(): number[] {
var result = new Array<number>();
this.toArray(result, 0);
return result;
}
/**
* Returns the luminance value
* @returns a float value
*/
public toLuminance(): number {
return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
}
/**
* Multiply each Color3 rgb values by the given Color3 rgb values in a new Color3 object
* @param otherColor defines the second operand
* @returns the new Color3 object
*/
public multiply(otherColor: DeepImmutable<Color3>): Color3 {
return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
}
/**
* Multiply the rgb values of the Color3 and the given Color3 and stores the result in the object "result"
* @param otherColor defines the second operand
* @param result defines the Color3 object where to store the result
* @returns the current Color3
*/
public multiplyToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
result.r = this.r * otherColor.r;
result.g = this.g * otherColor.g;
result.b = this.b * otherColor.b;
return this;
}
/**
* Determines equality between Color3 objects
* @param otherColor defines the second operand
* @returns true if the rgb values are equal to the given ones
*/
public equals(otherColor: DeepImmutable<Color3>): boolean {
return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
}
/**
* Determines equality between the current Color3 object and a set of r,b,g values
* @param r defines the red component to check
* @param g defines the green component to check
* @param b defines the blue component to check
* @returns true if the rgb values are equal to the given ones
*/
public equalsFloats(r: number, g: number, b: number): boolean {
return this.r === r && this.g === g && this.b === b;
}
/**
* Multiplies in place each rgb value by scale
* @param scale defines the scaling factor
* @returns the updated Color3
*/
public scale(scale: number): Color3 {
return new Color3(this.r * scale, this.g * scale, this.b * scale);
}
/**
* Multiplies the rgb values by scale and stores the result into "result"
* @param scale defines the scaling factor
* @param result defines the Color3 object where to store the result
* @returns the unmodified current Color3
*/
public scaleToRef(scale: number, result: Color3): Color3 {
result.r = this.r * scale;
result.g = this.g * scale;
result.b = this.b * scale;
return this;
}
/**
* Scale the current Color3 values by a factor and add the result to a given Color3
* @param scale defines the scale factor
* @param result defines color to store the result into
* @returns the unmodified current Color3
*/
public scaleAndAddToRef(scale: number, result: Color3): Color3 {
result.r += this.r * scale;
result.g += this.g * scale;
result.b += this.b * scale;
return this;
}
/**
* Clamps the rgb values by the min and max values and stores the result into "result"
* @param min defines minimum clamping value (default is 0)
* @param max defines maximum clamping value (default is 1)
* @param result defines color to store the result into
* @returns the original Color3
*/
public clampToRef(min: number = 0, max: number = 1, result: Color3): Color3 {
result.r = Scalar.Clamp(this.r, min, max);
result.g = Scalar.Clamp(this.g, min, max);
result.b = Scalar.Clamp(this.b, min, max);
return this;
}
/**
* Creates a new Color3 set with the added values of the current Color3 and of the given one
* @param otherColor defines the second operand
* @returns the new Color3
*/
public add(otherColor: DeepImmutable<Color3>): Color3 {
return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
}
/**
* Stores the result of the addition of the current Color3 and given one rgb values into "result"
* @param otherColor defines the second operand
* @param result defines Color3 object to store the result into
* @returns the unmodified current Color3
*/
public addToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
result.r = this.r + otherColor.r;
result.g = this.g + otherColor.g;
result.b = this.b + otherColor.b;
return this;
}
/**
* Returns a new Color3 set with the subtracted values of the given one from the current Color3
* @param otherColor defines the second operand
* @returns the new Color3
*/
public subtract(otherColor: DeepImmutable<Color3>): Color3 {
return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
}
/**
* Stores the result of the subtraction of given one from the current Color3 rgb values into "result"
* @param otherColor defines the second operand
* @param result defines Color3 object to store the result into
* @returns the unmodified current Color3
*/
public subtractToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
result.r = this.r - otherColor.r;
result.g = this.g - otherColor.g;
result.b = this.b - otherColor.b;
return this;
}
/**
* Copy the current object
* @returns a new Color3 copied the current one
*/
public clone(): Color3 {
return new Color3(this.r, this.g, this.b);
}
/**
* Copies the rgb values from the source in the current Color3
* @param source defines the source Color3 object
* @returns the updated Color3 object
*/
public copyFrom(source: DeepImmutable<Color3>): Color3 {
this.r = source.r;
this.g = source.g;
this.b = source.b;
return this;
}
/**
* Updates the Color3 rgb values from the given floats
* @param r defines the red component to read from
* @param g defines the green component to read from
* @param b defines the blue component to read from
* @returns the current Color3 object
*/
public copyFromFloats(r: number, g: number, b: number): Color3 {
this.r = r;
this.g = g;
this.b = b;
return this;
}
/**
* Updates the Color3 rgb values from the given floats
* @param r defines the red component to read from
* @param g defines the green component to read from
* @param b defines the blue component to read from
* @returns the current Color3 object
*/
public set(r: number, g: number, b: number): Color3 {
return this.copyFromFloats(r, g, b);
}
/**
* Compute the Color3 hexadecimal code as a string
* @returns a string containing the hexadecimal representation of the Color3 object
*/
public toHexString(): string {
var intR = (this.r * 255) | 0;
var intG = (this.g * 255) | 0;
var intB = (this.b * 255) | 0;
return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
}
/**
* Computes a new Color3 converted from the current one to linear space
* @returns a new Color3 object
*/
public toLinearSpace(): Color3 {
var convertedColor = new Color3();
this.toLinearSpaceToRef(convertedColor);
return convertedColor;
}
/**
* Converts current color in rgb space to HSV values
* @returns a new color3 representing the HSV values
*/
public toHSV(): Color3 {
let result = new Color3();
this.toHSVToRef(result);
return result;
}
/**
* Converts current color in rgb space to HSV values
* @param result defines the Color3 where to store the HSV values
*/
public toHSVToRef(result: Color3) {
var r = this.r;
var g = this.g;
var b = this.b;
var max = Math.max(r, g, b);
var min = Math.min(r, g, b);
var h = 0;
var s = 0;
var v = max;
var dm = max - min;
if (max !== 0) {
s = dm / max;
}
if (max != min) {
if (max == r) {
h = (g - b) / dm;
if (g < b) {
h += 6;
}
} else if (max == g) {
h = (b - r) / dm + 2;
} else if (max == b) {
h = (r - g) / dm + 4;
}
h *= 60;
}
result.r = h;
result.g = s;
result.b = v;
}
/**
* Converts the Color3 values to linear space and stores the result in "convertedColor"
* @param convertedColor defines the Color3 object where to store the linear space version
* @returns the unmodified Color3
*/
public toLinearSpaceToRef(convertedColor: Color3): Color3 {
convertedColor.r = Math.pow(this.r, ToLinearSpace);
convertedColor.g = Math.pow(this.g, ToLinearSpace);
convertedColor.b = Math.pow(this.b, ToLinearSpace);
return this;
}
/**
* Computes a new Color3 converted from the current one to gamma space
* @returns a new Color3 object
*/
public toGammaSpace(): Color3 {
var convertedColor = new Color3();
this.toGammaSpaceToRef(convertedColor);
return convertedColor;
}
/**
* Converts the Color3 values to gamma space and stores the result in "convertedColor"
* @param convertedColor defines the Color3 object where to store the gamma space version
* @returns the unmodified Color3
*/
public toGammaSpaceToRef(convertedColor: Color3): Color3 {
convertedColor.r = Math.pow(this.r, ToGammaSpace);
convertedColor.g = Math.pow(this.g, ToGammaSpace);
convertedColor.b = Math.pow(this.b, ToGammaSpace);
return this;
}
// Statics
private static _BlackReadOnly = Color3.Black() as DeepImmutable<Color3>;
/**
* Convert Hue, saturation and value to a Color3 (RGB)
* @param hue defines the hue
* @param saturation defines the saturation
* @param value defines the value
* @param result defines the Color3 where to store the RGB values
*/
public static HSVtoRGBToRef(hue: number, saturation: number, value: number, result: Color3) {
var chroma = value * saturation;
var h = hue / 60;
var x = chroma * (1 - Math.abs((h % 2) - 1));
var r = 0;
var g = 0;
var b = 0;
if (h >= 0 && h <= 1) {
r = chroma;
g = x;
} else if (h >= 1 && h <= 2) {
r = x;
g = chroma;
} else if (h >= 2 && h <= 3) {
g = chroma;
b = x;
} else if (h >= 3 && h <= 4) {
g = x;
b = chroma;
} else if (h >= 4 && h <= 5) {
r = x;
b = chroma;
} else if (h >= 5 && h <= 6) {
r = chroma;
b = x;
}
var m = value - chroma;
result.set((r + m), (g + m), (b + m));
}
/**
* Creates a new Color3 from the string containing valid hexadecimal values
* @param hex defines a string containing valid hexadecimal values
* @returns a new Color3 object
*/
public static FromHexString(hex: string): Color3 {
if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
return new Color3(0, 0, 0);
}
var r = parseInt(hex.substring(1, 3), 16);
var g = parseInt(hex.substring(3, 5), 16);
var b = parseInt(hex.substring(5, 7), 16);
return Color3.FromInts(r, g, b);
}
/**
* Creates a new Color3 from the starting index of the given array
* @param array defines the source array
* @param offset defines an offset in the source array
* @returns a new Color3 object
*/
public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color3 {
return new Color3(array[offset], array[offset + 1], array[offset + 2]);
}
/**
* Creates a new Color3 from integer values (< 256)
* @param r defines the red component to read from (value between 0 and 255)
* @param g defines the green component to read from (value between 0 and 255)
* @param b defines the blue component to read from (value between 0 and 255)
* @returns a new Color3 object
*/
public static FromInts(r: number, g: number, b: number): Color3 {
return new Color3(r / 255.0, g / 255.0, b / 255.0);
}
/**
* Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
* @param start defines the start Color3 value
* @param end defines the end Color3 value
* @param amount defines the gradient value between start and end
* @returns a new Color3 object
*/
public static Lerp(start: DeepImmutable<Color3>, end: DeepImmutable<Color3>, amount: number): Color3 {
var result = new Color3(0.0, 0.0, 0.0);
Color3.LerpToRef(start, end, amount, result);
return result;
}
/**
* Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
* @param left defines the start value
* @param right defines the end value
* @param amount defines the gradient factor
* @param result defines the Color3 object where to store the result
*/
public static LerpToRef(left: DeepImmutable<Color3>, right: DeepImmutable<Color3>, amount: number, result: Color3): void {
result.r = left.r + ((right.r - left.r) * amount);
result.g = left.g + ((right.g - left.g) * amount);
result.b = left.b + ((right.b - left.b) * amount);
}
/**
* Returns a Color3 value containing a red color
* @returns a new Color3 object
*/
public static Red(): Color3 { return new Color3(1, 0, 0); }
/**
* Returns a Color3 value containing a green color
* @returns a new Color3 object
*/
public static Green(): Color3 { return new Color3(0, 1, 0); }
/**
* Returns a Color3 value containing a blue color
* @returns a new Color3 object
*/
public static Blue(): Color3 { return new Color3(0, 0, 1); }
/**
* Returns a Color3 value containing a black color
* @returns a new Color3 object
*/
public static Black(): Color3 { return new Color3(0, 0, 0); }
/**
* Gets a Color3 value containing a black color that must not be updated
*/
public static get BlackReadOnly(): DeepImmutable<Color3> {
return Color3._BlackReadOnly;
}
/**
* Returns a Color3 value containing a white color
* @returns a new Color3 object
*/
public static White(): Color3 { return new Color3(1, 1, 1); }
/**
* Returns a Color3 value containing a purple color
* @returns a new Color3 object
*/
public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
/**
* Returns a Color3 value containing a magenta color
* @returns a new Color3 object
*/
public static Magenta(): Color3 { return new Color3(1, 0, 1); }
/**
* Returns a Color3 value containing a yellow color
* @returns a new Color3 object
*/
public static Yellow(): Color3 { return new Color3(1, 1, 0); }
/**
* Returns a Color3 value containing a gray color
* @returns a new Color3 object
*/
public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
/**
* Returns a Color3 value containing a teal color
* @returns a new Color3 object
*/
public static Teal(): Color3 { return new Color3(0, 1.0, 1.0); }
/**
* Returns a Color3 value containing a random color
* @returns a new Color3 object
*/
public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
}
/**
* Class used to hold a RBGA color
*/
export class Color4 {
/**
* Creates a new Color4 object from red, green, blue values, all between 0 and 1
* @param r defines the red component (between 0 and 1, default is 0)
* @param g defines the green component (between 0 and 1, default is 0)
* @param b defines the blue component (between 0 and 1, default is 0)
* @param a defines the alpha component (between 0 and 1, default is 1)
*/
constructor(
/**
* Defines the red component (between 0 and 1, default is 0)
*/
public r: number = 0,
/**
* Defines the green component (between 0 and 1, default is 0)
*/
public g: number = 0,
/**
* Defines the blue component (between 0 and 1, default is 0)
*/
public b: number = 0,
/**
* Defines the alpha component (between 0 and 1, default is 1)
*/
public a: number = 1) {
}
// Operators
/**
* Adds in place the given Color4 values to the current Color4 object
* @param right defines the second operand
* @returns the current updated Color4 object
*/
public addInPlace(right: DeepImmutable<Color4>): Color4 {
this.r += right.r;
this.g += right.g;
this.b += right.b;
this.a += right.a;
return this;
}
/**
* Creates a new array populated with 4 numeric elements : red, green, blue, alpha values
* @returns the new array
*/
public asArray(): number[] {
var result = new Array<number>();
this.toArray(result, 0);
return result;
}
/**
* Stores from the starting index in the given array the Color4 successive values
* @param array defines the array where to store the r,g,b components
* @param index defines an optional index in the target array to define where to start storing values
* @returns the current Color4 object
*/
public toArray(array: number[], index: number = 0): Color4 {
array[index] = this.r;
array[index + 1] = this.g;
array[index + 2] = this.b;
array[index + 3] = this.a;
return this;
}
/**
* Determines equality between Color4 objects
* @param otherColor defines the second operand
* @returns true if the rgba values are equal to the given ones
*/
public equals(otherColor: DeepImmutable<Color4>): boolean {
return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b && this.a === otherColor.a;
}
/**
* Creates a new Color4 set with the added values of the current Color4 and of the given one
* @param right defines the second operand
* @returns a new Color4 object
*/
public add(right: DeepImmutable<Color4>): Color4 {
return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
}
/**
* Creates a new Color4 set with the subtracted values of the given one from the current Color4
* @param right defines the second operand
* @returns a new Color4 object
*/
public subtract(right: DeepImmutable<Color4>): Color4 {
return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
}
/**
* Subtracts the given ones from the current Color4 values and stores the results in "result"
* @param right defines the second operand
* @param result defines the Color4 object where to store the result
* @returns the current Color4 object
*/
public subtractToRef(right: DeepImmutable<Color4>, result: Color4): Color4 {
result.r = this.r - right.r;
result.g = this.g - right.g;
result.b = this.b - right.b;
result.a = this.a - right.a;
return this;
}
/**
* Creates a new Color4 with the current Color4 values multiplied by scale
* @param scale defines the scaling factor to apply
* @returns a new Color4 object
*/
public scale(scale: number): Color4 {
return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
}
/**
* Multiplies the current Color4 values by scale and stores the result in "result"
* @param scale defines the scaling factor to apply
* @param result defines the Color4 object where to store the result
* @returns the current unmodified Color4
*/
public scaleToRef(scale: number, result: Color4): Color4 {
result.r = this.r * scale;
result.g = this.g * scale;
result.b = this.b * scale;
result.a = this.a * scale;
return this;
}
/**
* Scale the current Color4 values by a factor and add the result to a given Color4
* @param scale defines the scale factor
* @param result defines the Color4 object where to store the result
* @returns the unmodified current Color4
*/
public scaleAndAddToRef(scale: number, result: Color4): Color4 {
result.r += this.r * scale;
result.g += this.g * scale;
result.b += this.b * scale;
result.a += this.a * scale;
return this;
}
/**
* Clamps the rgb values by the min and max values and stores the result into "result"
* @param min defines minimum clamping value (default is 0)
* @param max defines maximum clamping value (default is 1)
* @param result defines color to store the result into.
* @returns the cuurent Color4
*/
public clampToRef(min: number = 0, max: number = 1, result: Color4): Color4 {
result.r = Scalar.Clamp(this.r, min, max);
result.g = Scalar.Clamp(this.g, min, max);
result.b = Scalar.Clamp(this.b, min, max);
result.a = Scalar.Clamp(this.a, min, max);
return this;
}
/**
* Multipy an Color4 value by another and return a new Color4 object
* @param color defines the Color4 value to multiply by
* @returns a new Color4 object
*/
public multiply(color: Color4): Color4 {
return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
}
/**
* Multipy a Color4 value by another and push the result in a reference value
* @param color defines the Color4 value to multiply by
* @param result defines the Color4 to fill the result in
* @returns the result Color4
*/
public multiplyToRef(color: Color4, result: Color4): Color4 {
result.r = this.r * color.r;
result.g = this.g * color.g;
result.b = this.b * color.b;
result.a = this.a * color.a;
return result;
}
/**
* Creates a string with the Color4 current values
* @returns the string representation of the Color4 object
*/
public toString(): string {
return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
}
/**
* Returns the string "Color4"
* @returns "Color4"
*/
public getClassName(): string {
return "Color4";
}
/**
* Compute the Color4 hash code
* @returns an unique number that can be used to hash Color4 objects
*/
public getHashCode(): number {
let hash = (this.r * 255) || 0;
hash = (hash * 397) ^ ((this.g * 255) || 0);
hash = (hash * 397) ^ ((this.b * 255) || 0);
hash = (hash * 397) ^ ((this.a * 255) || 0);
return hash;
}
/**
* Creates a new Color4 copied from the current one
* @returns a new Color4 object
*/
public clone(): Color4 {
return new Color4(this.r, this.g, this.b, this.a);
}
/**
* Copies the given Color4 values into the current one
* @param source defines the source Color4 object
* @returns the current updated Color4 object
*/
public copyFrom(source: Color4): Color4 {
this.r = source.r;
this.g = source.g;
this.b = source.b;
this.a = source.a;
return this;
}
/**
* Copies the given float values into the current one
* @param r defines the red component to read from
* @param g defines the green component to read from
* @param b defines the blue component to read from
* @param a defines the alpha component to read from
* @returns the current updated Color4 object
*/
public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
return this;
}
/**
* Copies the given float values into the current one
* @param r defines the red component to read from
* @param g defines the green component to read from
* @param b defines the blue component to read from
* @param a defines the alpha component to read from
* @returns the current updated Color4 object
*/
public set(r: number, g: number, b: number, a: number): Color4 {
return this.copyFromFloats(r, g, b, a);
}
/**
* Compute the Color4 hexadecimal code as a string
* @returns a string containing the hexadecimal representation of the Color4 object
*/
public toHexString(): string {
var intR = (this.r * 255) | 0;
var intG = (this.g * 255) | 0;
var intB = (this.b * 255) | 0;
var intA = (this.a * 255) | 0;
return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB) + Scalar.ToHex(intA);
}
/**
* Computes a new Color4 converted from the current one to linear space
* @returns a new Color4 object
*/
public toLinearSpace(): Color4 {
var convertedColor = new Color4();
this.toLinearSpaceToRef(convertedColor);
return convertedColor;
}
/**
* Converts the Color4 values to linear space and stores the result in "convertedColor"
* @param convertedColor defines the Color4 object where to store the linear space version
* @returns the unmodified Color4
*/
public toLinearSpaceToRef(convertedColor: Color4): Color4 {
convertedColor.r = Math.pow(this.r, ToLinearSpace);
convertedColor.g = Math.pow(this.g, ToLinearSpace);
convertedColor.b = Math.pow(this.b, ToLinearSpace);
convertedColor.a = this.a;
return this;
}
/**
* Computes a new Color4 converted from the current one to gamma space
* @returns a new Color4 object
*/
public toGammaSpace(): Color4 {
var convertedColor = new Color4();
this.toGammaSpaceToRef(convertedColor);
return convertedColor;
}
/**
* Converts the Color4 values to gamma space and stores the result in "convertedColor"
* @param convertedColor defines the Color4 object where to store the gamma space version
* @returns the unmodified Color4
*/
public toGammaSpaceToRef(convertedColor: Color4): Color4 {
convertedColor.r = Math.pow(this.r, ToGammaSpace);
convertedColor.g = Math.pow(this.g, ToGammaSpace);
convertedColor.b = Math.pow(this.b, ToGammaSpace);
convertedColor.a = this.a;
return this;
}
// Statics
/**
* Creates a new Color4 from the string containing valid hexadecimal values
* @param hex defines a string containing valid hexadecimal values
* @returns a new Color4 object
*/
public static FromHexString(hex: string): Color4 {
if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
return new Color4(0.0, 0.0, 0.0, 0.0);
}
var r = parseInt(hex.substring(1, 3), 16);
var g = parseInt(hex.substring(3, 5), 16);
var b = parseInt(hex.substring(5, 7), 16);
var a = parseInt(hex.substring(7, 9), 16);
return Color4.FromInts(r, g, b, a);
}
/**
* Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
* @param left defines the start value
* @param right defines the end value
* @param amount defines the gradient factor
* @returns a new Color4 object
*/
public static Lerp(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number): Color4 {
var result = new Color4(0.0, 0.0, 0.0, 0.0);
Color4.LerpToRef(left, right, amount, result);
return result;
}
/**
* Set the given "result" with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
* @param left defines the start value
* @param right defines the end value
* @param amount defines the gradient factor
* @param result defines the Color4 object where to store data
*/
public static LerpToRef(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number, result: Color4): void {
result.r = left.r + (right.r - left.r) * amount;
result.g = left.g + (right.g - left.g) * amount;
result.b = left.b + (right.b - left.b) * amount;
result.a = left.a + (right.a - left.a) * amount;
}
/**
* Creates a new Color4 from a Color3 and an alpha value
* @param color3 defines the source Color3 to read from
* @param alpha defines the alpha component (1.0 by default)
* @returns a new Color4 object
*/
public static FromColor3(color3: DeepImmutable<Color3>, alpha: number = 1.0): Color4 {
return new Color4(color3.r, color3.g, color3.b, alpha);
}
/**
* Creates a new Color4 from the starting index element of the given array
* @param array defines the source array to read from
* @param offset defines the offset in the source array
* @returns a new Color4 object
*/
public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color4 {
return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
}
/**
* Creates a new Color3 from integer values (< 256)
* @param r defines the red component to read from (value between 0 and 255)
* @param g defines the green component to read from (value between 0 and 255)
* @param b defines the blue component to read from (value between 0 and 255)
* @param a defines the alpha component to read from (value between 0 and 255)
* @returns a new Color3 object
*/
public static FromInts(r: number, g: number, b: number, a: number): Color4 {
return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
}
/**
* Check the content of a given array and convert it to an array containing RGBA data
* If the original array was already containing count * 4 values then it is returned directly
* @param colors defines the array to check
* @param count defines the number of RGBA data to expect
* @returns an array containing count * 4 values (RGBA)
*/
public static CheckColors4(colors: number[], count: number): number[] {
// Check if color3 was used
if (colors.length === count * 3) {
var colors4 = [];
for (var index = 0; index < colors.length; index += 3) {
var newIndex = (index / 3) * 4;
colors4[newIndex] = colors[index];
colors4[newIndex + 1] = colors[index + 1];
colors4[newIndex + 2] = colors[index + 2];
colors4[newIndex + 3] = 1.0;
}
return colors4;
}
return colors;
}
}