Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / base / color.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 base.exportTo('base', function() {
8   function Color(opt_r, opt_g, opt_b, opt_a) {
9     this.r = Math.floor(opt_r) || 0;
10     this.g = Math.floor(opt_g) || 0;
11     this.b = Math.floor(opt_b) || 0;
12     this.a = opt_a;
13   }
14
15   Color.fromString = function(str) {
16     var tmp;
17     var values;
18     if (str.substr(0, 4) == 'rgb(') {
19       tmp = str.substr(4, str.length - 5);
20       values = tmp.split(',').map(function(v) {
21         return v.replace(/^\s+/, '', 'g');
22       });
23       if (values.length != 3)
24         throw new Error('Malformatted rgb-expression');
25       return new Color(
26           parseInt(values[0]),
27           parseInt(values[1]),
28           parseInt(values[2]));
29     } else if (str.substr(0, 5) == 'rgba(') {
30       tmp = str.substr(5, str.length - 6);
31       values = tmp.split(',').map(function(v) {
32         return v.replace(/^\s+/, '', 'g');
33       });
34       if (values.length != 4)
35         throw new Error('Malformatted rgb-expression');
36       return new Color(
37           parseInt(values[0]),
38           parseInt(values[1]),
39           parseInt(values[2]),
40           parseFloat(values[3]));
41     } else if (str[0] == '#' && str.length == 7) {
42       return new Color(
43           parseInt(str.substr(1, 2), 16),
44           parseInt(str.substr(3, 2), 16),
45           parseInt(str.substr(5, 2), 16));
46     } else {
47       throw new Error('Unrecognized string format.');
48     }
49   };
50
51   Color.lerp = function(a, b, percent) {
52     if (a.a !== undefined && b.a !== undefined)
53       return Color.lerpRGBA(a, b, percent);
54     return Color.lerpRGB(a, b, percent);
55   }
56   Color.lerpRGB = function(a, b, percent) {
57     return new Color(
58         ((b.r - a.r) * percent) + a.r,
59         ((b.g - a.g) * percent) + a.g,
60         ((b.b - a.b) * percent) + a.b);
61   }
62
63   Color.lerpRGBA = function(a, b, percent) {
64     return new Color(
65         ((b.r - a.r) * percent) + a.r,
66         ((b.g - a.g) * percent) + a.g,
67         ((b.b - a.b) * percent) + a.b,
68         ((b.a - a.a) * percent) + a.a);
69   }
70
71   Color.prototype = {
72     clone: function() {
73       var c = new Color();
74       c.r = this.r;
75       c.g = this.g;
76       c.b = this.b;
77       c.a = this.a;
78       return c;
79     },
80
81     brighten: function(opt_k) {
82       var k;
83       k = opt_k || 0.45;
84
85       return new Color(
86           Math.min(255, this.r + Math.floor(this.r * k)),
87           Math.min(255, this.g + Math.floor(this.g * k)),
88           Math.min(255, this.b + Math.floor(this.b * k)));
89     },
90
91     darken: function(opt_k) {
92       var k;
93       k = opt_k || 0.45;
94
95       return new Color(
96           Math.min(255, this.r - Math.floor(this.r * k)),
97           Math.min(255, this.g - Math.floor(this.g * k)),
98           Math.min(255, this.b - Math.floor(this.b * k)));
99     },
100
101     withAlpha: function(a) {
102       return new Color(this.r, this.g, this.b, a);
103     },
104
105     toString: function() {
106       if (this.a !== undefined) {
107         return 'rgba(' +
108             this.r + ',' + this.g + ',' +
109             this.b + ',' + this.a + ')';
110       }
111       return 'rgb(' + this.r + ',' + this.g + ',' + this.b + ')';
112     }
113   };
114
115   return {
116     Color: Color
117   };
118 });