Skip to content

Commit 951cb09

Browse files
author
Google Earth Engine Authors
committed
Launch ee.Color and ee.Palette client support.
PiperOrigin-RevId: 887426251
1 parent 280686b commit 951cb09

File tree

10 files changed

+1706
-42
lines changed

10 files changed

+1706
-42
lines changed

javascript/src/color.js

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/**
2+
* @fileoverview A wrapper for colors.
3+
* @suppress {useOfGoogProvide}
4+
*/
5+
6+
goog.provide('ee.Color');
7+
8+
goog.require('ee.ApiFunction');
9+
goog.require('ee.ComputedObject');
10+
11+
12+
13+
/**
14+
* An object to represent colors.
15+
*
16+
* @param {string|!Array<number>|!ee.ComputedObject} color
17+
* 1) A W3C compatible color string.
18+
* 2) A list of RGBA values in the range of [0:1].
19+
* 3) A ComputedObject returning a color.
20+
* @constructor
21+
* @extends {ee.ComputedObject}
22+
* @export
23+
*/
24+
ee.Color = function(color) {
25+
if (!(this instanceof ee.Color)) {
26+
return new ee.Color(color);
27+
}
28+
29+
ee.Color.initialize();
30+
31+
if (color instanceof ee.ComputedObject) {
32+
ee.Color.base(this, 'constructor', color.func, color.args, color.varName);
33+
} else {
34+
ee.Color.base(
35+
this, 'constructor', new ee.ApiFunction('Color'), {'input': color});
36+
}
37+
};
38+
goog.inherits(ee.Color, ee.ComputedObject);
39+
40+
41+
/**
42+
* Whether the class has been initialized.
43+
* @type {boolean}
44+
* @private
45+
*/
46+
ee.Color.initialized_ = false;
47+
48+
49+
/**
50+
* Imports API functions to this class.
51+
*/
52+
ee.Color.initialize = function() {
53+
if (!ee.Color.initialized_) {
54+
ee.ApiFunction.importApi(ee.Color, 'Color', 'Color');
55+
ee.Color.initialized_ = true;
56+
}
57+
};
58+
59+
60+
/**
61+
* Removes imported API functions from this class.
62+
*/
63+
ee.Color.reset = function() {
64+
ee.ApiFunction.clearApi(ee.Color);
65+
ee.Color.initialized_ = false;
66+
};
67+
68+
69+
/**
70+
* @override
71+
* @return {string}
72+
*/
73+
ee.Color.prototype.name = function() {
74+
return 'Color';
75+
};
76+
77+
78+
/**
79+
* Creates a Color given a list of HSV values.
80+
*
81+
* @param {!Array<number>|!ee.ComputedObject} hsv A list of HSV (hue,
82+
* saturation, value) values in the range [0:1].
83+
* @return {!ee.Color}
84+
* @export
85+
*/
86+
ee.Color.fromHsv = function(hsv) {
87+
return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromHsv', hsv));
88+
};
89+
90+
91+
/**
92+
* Creates a Color given a list of HSL values.
93+
*
94+
* @param {!Array<number>|!ee.ComputedObject} hsl A list of HSL (hue,
95+
* saturation, luminosity) values in the range [0:1].
96+
* @return {!ee.Color}
97+
* @export
98+
*/
99+
ee.Color.fromHsl = function(hsl) {
100+
return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromHsl', hsl));
101+
};
102+
103+
104+
/**
105+
* Creates a Color given a list of CIE-LAB values.
106+
*
107+
* @param {!Array<number>|!ee.ComputedObject} lab A list of CIE-LAB values.
108+
* @return {!ee.Color}
109+
* @export
110+
*/
111+
ee.Color.fromLab = function(lab) {
112+
return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromLab', lab));
113+
};
114+
115+
116+
/**
117+
* Creates a Color given a list of CIE-LCH values.
118+
*
119+
* @param {!Array<number>|!ee.ComputedObject} lch A list of CIE-LCH (lightness,
120+
* chroma, hue) values.
121+
* @return {!ee.Color}
122+
* @export
123+
*/
124+
ee.Color.fromLch = function(lch) {
125+
return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromLch', lch));
126+
};
127+
128+
129+
/**
130+
* Creates a gray color.
131+
*
132+
* @param {number|!ee.ComputedObject} value The gray value in the range [0:1].
133+
* @param {number|!ee.ComputedObject=} opt_alpha The alpha value in the range
134+
* [0:1].
135+
* @return {!ee.Color}
136+
* @export
137+
*/
138+
ee.Color.gray = function(value, opt_alpha) {
139+
return /** @type {!ee.Color} */ (
140+
ee.ApiFunction._call('Color.gray', value, opt_alpha));
141+
};
142+
143+
144+
/**
145+
* Mixes two colors.
146+
*
147+
* @param {!ee.Color} start The starting color.
148+
* @param {!ee.Color} end The ending color.
149+
* @param {number|!ee.ComputedObject=} opt_ratio The mix ratio.
150+
* @param {string|!ee.ComputedObject=} opt_colorspace The colorspace to mix
151+
* in.
152+
* @return {!ee.Color}
153+
* @export
154+
*/
155+
ee.Color.mix = function(start, end, opt_ratio, opt_colorspace) {
156+
return /** @type {!ee.Color} */ (
157+
ee.ApiFunction._call('Color.mix', start, end, opt_ratio, opt_colorspace));
158+
};
159+
160+
161+
/**
162+
* Scale each of the RGB channels to produce a brighter color.
163+
*
164+
* @param {number|!ee.ComputedObject=} opt_scale The scale factor.
165+
* @return {!ee.Color}
166+
* @export
167+
*/
168+
ee.Color.prototype.brighter = function(opt_scale) {
169+
return /** @type {!ee.Color} */ (
170+
ee.ApiFunction._call('Color.brighter', this, opt_scale));
171+
};
172+
173+
174+
/**
175+
* Scale each of the RGB channels to produce a darker color.
176+
*
177+
* @param {number|!ee.ComputedObject=} opt_scale The scale factor.
178+
* @return {!ee.Color}
179+
* @export
180+
*/
181+
ee.Color.prototype.darker = function(opt_scale) {
182+
return /** @type {!ee.Color} */ (
183+
ee.ApiFunction._call('Color.darker', this, opt_scale));
184+
};
185+
186+
187+
/**
188+
* Convert a color to HSL.
189+
*
190+
* @return {!ee.ComputedObject}
191+
* @export
192+
*/
193+
ee.Color.prototype.toHsl = function() {
194+
return /** @type {!ee.ComputedObject} */ (
195+
ee.ApiFunction._call('Color.toHsl', this));
196+
};
197+
198+
199+
/**
200+
* Convert a color to HSV.
201+
*
202+
* @return {!ee.ComputedObject}
203+
* @export
204+
*/
205+
ee.Color.prototype.toHsv = function() {
206+
return /** @type {!ee.ComputedObject} */ (
207+
ee.ApiFunction._call('Color.toHsv', this));
208+
};
209+
210+
211+
/**
212+
* Convert a color to CIE-Lab.
213+
*
214+
* @return {!ee.ComputedObject}
215+
* @export
216+
*/
217+
ee.Color.prototype.toLab = function() {
218+
return /** @type {!ee.ComputedObject} */ (
219+
ee.ApiFunction._call('Color.toLab', this));
220+
};
221+
222+
223+
/**
224+
* Convert a color to CIE-LCH.
225+
*
226+
* @return {!ee.ComputedObject}
227+
* @export
228+
*/
229+
ee.Color.prototype.toLch = function() {
230+
return /** @type {!ee.ComputedObject} */ (
231+
ee.ApiFunction._call('Color.toLch', this));
232+
};
233+
234+
235+
/**
236+
* Convert a color to RGB.
237+
*
238+
* @return {!ee.ComputedObject}
239+
* @export
240+
*/
241+
ee.Color.prototype.toRgb = function() {
242+
return /** @type {!ee.ComputedObject} */ (
243+
ee.ApiFunction._call('Color.toRGB', this));
244+
};
245+
246+
247+
/**
248+
* Returns value of a color as an RGBA hex string.
249+
*
250+
* @return {!ee.ComputedObject}
251+
* @export
252+
*/
253+
ee.Color.prototype.toHexString = function() {
254+
return /** @type {!ee.ComputedObject} */ (
255+
ee.ApiFunction._call('Color.toHexString', this));
256+
};

javascript/src/ee.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ goog.provide('ee');
66

77
goog.require('ee.ApiFunction');
88
goog.require('ee.Collection');
9+
goog.require('ee.Color');
910
goog.require('ee.ComputedObject');
1011
goog.require('ee.CustomFunction');
1112
goog.require('ee.Date');
@@ -21,6 +22,7 @@ goog.require('ee.Image');
2122
goog.require('ee.ImageCollection');
2223
goog.require('ee.List');
2324
goog.require('ee.Number');
25+
goog.require('ee.Palette');
2426
goog.require('ee.String');
2527
goog.require('ee.Terrain');
2628
goog.require('ee.Types');
@@ -129,6 +131,7 @@ ee.reset = function() {
129131
ee.ready_ = ee.InitState.NOT_READY;
130132
ee.data.reset();
131133
ee.ApiFunction.reset();
134+
ee.Color.reset();
132135
ee.Date.reset();
133136
ee.Dictionary.reset();
134137
ee.Element.reset();
@@ -141,6 +144,7 @@ ee.reset = function() {
141144
ee.Geometry.reset();
142145
ee.List.reset();
143146
ee.Number.reset();
147+
ee.Palette.reset();
144148
ee.String.reset();
145149
ee.Terrain.reset();
146150
ee.resetGeneratedClasses_();

0 commit comments

Comments
 (0)