diff --git a/javascript/src/color.js b/javascript/src/color.js new file mode 100644 index 000000000..028b824ec --- /dev/null +++ b/javascript/src/color.js @@ -0,0 +1,256 @@ +/** + * @fileoverview A wrapper for colors. + * @suppress {useOfGoogProvide} + */ + +goog.provide('ee.Color'); + +goog.require('ee.ApiFunction'); +goog.require('ee.ComputedObject'); + + + +/** + * An object to represent colors. + * + * @param {string|!Array|!ee.ComputedObject} color + * 1) A W3C compatible color string. + * 2) A list of RGBA values in the range of [0:1]. + * 3) A ComputedObject returning a color. + * @constructor + * @extends {ee.ComputedObject} + * @export + */ +ee.Color = function(color) { + if (!(this instanceof ee.Color)) { + return new ee.Color(color); + } + + ee.Color.initialize(); + + if (color instanceof ee.ComputedObject) { + ee.Color.base(this, 'constructor', color.func, color.args, color.varName); + } else { + ee.Color.base( + this, 'constructor', new ee.ApiFunction('Color'), {'input': color}); + } +}; +goog.inherits(ee.Color, ee.ComputedObject); + + +/** + * Whether the class has been initialized. + * @type {boolean} + * @private + */ +ee.Color.initialized_ = false; + + +/** + * Imports API functions to this class. + */ +ee.Color.initialize = function() { + if (!ee.Color.initialized_) { + ee.ApiFunction.importApi(ee.Color, 'Color', 'Color'); + ee.Color.initialized_ = true; + } +}; + + +/** + * Removes imported API functions from this class. + */ +ee.Color.reset = function() { + ee.ApiFunction.clearApi(ee.Color); + ee.Color.initialized_ = false; +}; + + +/** + * @override + * @return {string} + */ +ee.Color.prototype.name = function() { + return 'Color'; +}; + + +/** + * Creates a Color given a list of HSV values. + * + * @param {!Array|!ee.ComputedObject} hsv A list of HSV (hue, + * saturation, value) values in the range [0:1]. + * @return {!ee.Color} + * @export + */ +ee.Color.fromHsv = function(hsv) { + return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromHsv', hsv)); +}; + + +/** + * Creates a Color given a list of HSL values. + * + * @param {!Array|!ee.ComputedObject} hsl A list of HSL (hue, + * saturation, luminosity) values in the range [0:1]. + * @return {!ee.Color} + * @export + */ +ee.Color.fromHsl = function(hsl) { + return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromHsl', hsl)); +}; + + +/** + * Creates a Color given a list of CIE-LAB values. + * + * @param {!Array|!ee.ComputedObject} lab A list of CIE-LAB values. + * @return {!ee.Color} + * @export + */ +ee.Color.fromLab = function(lab) { + return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromLab', lab)); +}; + + +/** + * Creates a Color given a list of CIE-LCH values. + * + * @param {!Array|!ee.ComputedObject} lch A list of CIE-LCH (lightness, + * chroma, hue) values. + * @return {!ee.Color} + * @export + */ +ee.Color.fromLch = function(lch) { + return /** @type {!ee.Color} */ (ee.ApiFunction._call('Color.fromLch', lch)); +}; + + +/** + * Creates a gray color. + * + * @param {number|!ee.ComputedObject} value The gray value in the range [0:1]. + * @param {number|!ee.ComputedObject=} opt_alpha The alpha value in the range + * [0:1]. + * @return {!ee.Color} + * @export + */ +ee.Color.gray = function(value, opt_alpha) { + return /** @type {!ee.Color} */ ( + ee.ApiFunction._call('Color.gray', value, opt_alpha)); +}; + + +/** + * Mixes two colors. + * + * @param {!ee.Color} start The starting color. + * @param {!ee.Color} end The ending color. + * @param {number|!ee.ComputedObject=} opt_ratio The mix ratio. + * @param {string|!ee.ComputedObject=} opt_colorspace The colorspace to mix + * in. + * @return {!ee.Color} + * @export + */ +ee.Color.mix = function(start, end, opt_ratio, opt_colorspace) { + return /** @type {!ee.Color} */ ( + ee.ApiFunction._call('Color.mix', start, end, opt_ratio, opt_colorspace)); +}; + + +/** + * Scale each of the RGB channels to produce a brighter color. + * + * @param {number|!ee.ComputedObject=} opt_scale The scale factor. + * @return {!ee.Color} + * @export + */ +ee.Color.prototype.brighter = function(opt_scale) { + return /** @type {!ee.Color} */ ( + ee.ApiFunction._call('Color.brighter', this, opt_scale)); +}; + + +/** + * Scale each of the RGB channels to produce a darker color. + * + * @param {number|!ee.ComputedObject=} opt_scale The scale factor. + * @return {!ee.Color} + * @export + */ +ee.Color.prototype.darker = function(opt_scale) { + return /** @type {!ee.Color} */ ( + ee.ApiFunction._call('Color.darker', this, opt_scale)); +}; + + +/** + * Convert a color to HSL. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toHsl = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toHsl', this)); +}; + + +/** + * Convert a color to HSV. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toHsv = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toHsv', this)); +}; + + +/** + * Convert a color to CIE-Lab. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toLab = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toLab', this)); +}; + + +/** + * Convert a color to CIE-LCH. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toLch = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toLch', this)); +}; + + +/** + * Convert a color to RGB. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toRgb = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toRGB', this)); +}; + + +/** + * Returns value of a color as an RGBA hex string. + * + * @return {!ee.ComputedObject} + * @export + */ +ee.Color.prototype.toHexString = function() { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Color.toHexString', this)); +}; diff --git a/javascript/src/ee.js b/javascript/src/ee.js index 241ee95c1..c5c67a964 100644 --- a/javascript/src/ee.js +++ b/javascript/src/ee.js @@ -6,6 +6,7 @@ goog.provide('ee'); goog.require('ee.ApiFunction'); goog.require('ee.Collection'); +goog.require('ee.Color'); goog.require('ee.ComputedObject'); goog.require('ee.CustomFunction'); goog.require('ee.Date'); @@ -21,6 +22,7 @@ goog.require('ee.Image'); goog.require('ee.ImageCollection'); goog.require('ee.List'); goog.require('ee.Number'); +goog.require('ee.Palette'); goog.require('ee.String'); goog.require('ee.Terrain'); goog.require('ee.Types'); @@ -129,6 +131,7 @@ ee.reset = function() { ee.ready_ = ee.InitState.NOT_READY; ee.data.reset(); ee.ApiFunction.reset(); + ee.Color.reset(); ee.Date.reset(); ee.Dictionary.reset(); ee.Element.reset(); @@ -141,6 +144,7 @@ ee.reset = function() { ee.Geometry.reset(); ee.List.reset(); ee.Number.reset(); + ee.Palette.reset(); ee.String.reset(); ee.Terrain.reset(); ee.resetGeneratedClasses_(); diff --git a/javascript/src/palette.js b/javascript/src/palette.js new file mode 100644 index 000000000..abf0937a6 --- /dev/null +++ b/javascript/src/palette.js @@ -0,0 +1,239 @@ +/** + * @fileoverview A wrapper for palettes. + * @suppress {useOfGoogProvide} + */ + +goog.provide('ee.Palette'); + +goog.require('ee.ApiFunction'); +goog.require('ee.Color'); +goog.require('ee.ComputedObject'); + + + +/** + * An object to represent palettes. + * + * @param {string|!Array|!ee.ComputedObject|!Object=} opt_colors + * A list of colors or the name of a predefined color palette. + * @param {string=} opt_mode The colorspace in which to interpolate. + * @param {number=} opt_min The minimum value of the palette. + * @param {number=} opt_max The maximum value of the palette. + * @param {!Array=} opt_padding Shifts the color range by padding the + * end. + * @param {number|!Array=} opt_classes Create a palette representing + * discrete classes. + * @param {!Array=} opt_positions Set the positions for the colors. + * @param {boolean=} opt_correctLightness Correct the color spacing to spread + * lightness range. + * @param {number=} opt_gamma A gamma correction for the palette. + * @param {boolean=} opt_bezier Sets the palette to use Bezier interpolation. + * @constructor + * @extends {ee.ComputedObject} + * @export + */ +ee.Palette = function( + opt_colors, opt_mode, opt_min, opt_max, opt_padding, opt_classes, + opt_positions, opt_correctLightness, opt_gamma, opt_bezier) { + if (!(this instanceof ee.Palette)) { + return new ee.Palette( + opt_colors, opt_mode, opt_min, opt_max, opt_padding, opt_classes, + opt_positions, opt_correctLightness, opt_gamma, opt_bezier); + } + + ee.Palette.initialize(); + + if (opt_colors instanceof ee.ComputedObject && arguments.length == 1) { + ee.Palette.base( + this, 'constructor', opt_colors.func, opt_colors.args, + opt_colors.varName); + } else { + ee.Palette.base(this, 'constructor', new ee.ApiFunction('Palette'), { + 'colors': opt_colors, + 'mode': opt_mode || 'RGB', + 'min': opt_min != null ? opt_min : 0.0, + 'max': opt_max != null ? opt_max : 1.0, + 'padding': opt_padding || null, + 'classes': opt_classes || null, + 'positions': opt_positions || null, + 'correctLightness': opt_correctLightness || false, + 'gamma': opt_gamma != null ? opt_gamma : 1.0, + 'bezier': opt_bezier || false + }); + } +}; +goog.inherits(ee.Palette, ee.ComputedObject); + + +/** + * Whether the class has been initialized. + * @type {boolean} + * @private + */ +ee.Palette.initialized_ = false; + + +/** + * Imports API functions to this class. + */ +ee.Palette.initialize = function() { + if (!ee.Palette.initialized_) { + ee.ApiFunction.importApi(ee.Palette, 'Palette', 'Palette'); + ee.Palette.initialized_ = true; + } +}; + + +/** + * Removes imported API functions from this class. + */ +ee.Palette.reset = function() { + ee.ApiFunction.clearApi(ee.Palette); + ee.Palette.initialized_ = false; +}; + + +/** + * @override + * @return {string} + */ +ee.Palette.prototype.name = function() { + return 'Palette'; +}; + + +/** + * Returns the color at the given value. + * + * @param {number|!ee.ComputedObject} value The value to look up. + * @return {!ee.Color} + * @export + */ +ee.Palette.prototype.getColor = function(value) { + return /** @type {!ee.Color} */ ( + ee.ApiFunction._call('Palette.getColor', this, value)); +}; + + +/** + * Get colors from this palette. + * + * @param {number|!ee.ComputedObject=} opt_nColors The number of equally + * spaced colors to retrieve. + * @return {!ee.ComputedObject} + * @export + */ +ee.Palette.prototype.getColors = function(opt_nColors) { + return /** @type {!ee.ComputedObject} */ ( + ee.ApiFunction._call('Palette.getColors', this, opt_nColors)); +}; + + +/** + * Set the colorspace interpolation mode. + * + * @param {string|!ee.ComputedObject} mode The colorspace mode. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.mode = function(mode) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.mode', this, mode)); +}; + + +/** + * Set the minimum and maximum limits for the palette. + * + * @param {number|!ee.ComputedObject} min The minimum value. + * @param {number|!ee.ComputedObject} max The maximum value. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.limits = function(min, max) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.limits', this, min, max)); +}; + + +/** + * Set the position for each color in the palette. + * + * @param {!Array|!ee.ComputedObject} positions A list of values + * specifying the position for each color. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.positions = function(positions) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.positions', this, positions)); +}; + + +/** + * Use discrete classes as opposed to a continuous gradient. + * + * @param {number|!Array|!ee.ComputedObject} classes + * Either a list of class break values, or a single number. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.classes = function(classes) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.classes', this, classes)); +}; + + +/** + * Shifts the color range by padding the end of the color scale. + * + * @param {number|!ee.ComputedObject} left The left padding. + * @param {number|!ee.ComputedObject=} opt_right The right padding. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.padding = function(left, opt_right) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.padding', this, left, opt_right)); +}; + + +/** + * Apply a gamma correction to the palette. + * + * @param {number|!ee.ComputedObject} gamma The gamma value. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.gamma = function(gamma) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.gamma', this, gamma)); +}; + + +/** + * Sets the palette to use bezier interpolation. + * + * @param {boolean|!ee.ComputedObject=} opt_interpolate Whether to use bezier + * interpolation. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.bezier = function(opt_interpolate) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.bezier', this, opt_interpolate)); +}; + + +/** + * Correct the color spacing to spread lightness range evenly. + * + * @param {boolean|!ee.ComputedObject=} opt_correct Whether to correct + * lightness. + * @return {!ee.Palette} + * @export + */ +ee.Palette.prototype.correctLightness = function(opt_correct) { + return /** @type {!ee.Palette} */ ( + ee.ApiFunction._call('Palette.correctLightness', this, opt_correct)); +}; diff --git a/python/ee/__init__.py b/python/ee/__init__.py index c02420fdf..eabe38aac 100644 --- a/python/ee/__init__.py +++ b/python/ee/__init__.py @@ -33,6 +33,7 @@ from .classifier import Classifier from .clusterer import Clusterer from .collection import Collection +from .color import Color from .computedobject import ComputedObject from .confusionmatrix import ConfusionMatrix from .customfunction import CustomFunction @@ -57,6 +58,7 @@ from .join import Join from .kernel import Kernel from .model import Model +from .palette import Palette from .pixeltype import PixelType from .projection import Projection from .reducer import Reducer @@ -64,49 +66,51 @@ from .terrain import Terrain if TYPE_CHECKING: - __all__: tuple[str, ...] = ( - 'ServiceAccountCredentials', - 'ApiFunction', - 'Blob', - 'Classifier', - 'Clusterer', - 'Collection', - 'ComputedObject', - 'ConfusionMatrix', - 'CustomFunction', - 'DateRange', - 'Dictionary', - 'Array', - 'Date', - 'EEException', - 'List', - 'Number', - 'String', - 'Element', - 'Encodable', - 'ErrorMargin', - 'Feature', - 'FeatureCollection', - 'Filter', - 'Function', - 'Geometry', - 'Image', - 'ImageCollection', - 'Join', - 'Kernel', - 'Model', - 'PixelType', - 'Projection', - 'Reducer', - 'Serializer', - 'Terrain', - 'apply', - 'call', - 'profilePrinting', - ) + __all__: tuple[str, ...] = ( + 'ServiceAccountCredentials', + 'ApiFunction', + 'Blob', + 'Classifier', + 'Clusterer', + 'Collection', + 'Color', + 'ComputedObject', + 'ConfusionMatrix', + 'CustomFunction', + 'DateRange', + 'Dictionary', + 'Array', + 'Date', + 'EEException', + 'List', + 'Number', + 'String', + 'Element', + 'Encodable', + 'ErrorMargin', + 'Feature', + 'FeatureCollection', + 'Filter', + 'Function', + 'Geometry', + 'Image', + 'ImageCollection', + 'Join', + 'Kernel', + 'Model', + 'Palette', + 'PixelType', + 'Projection', + 'Reducer', + 'Serializer', + 'Terrain', + 'apply', + 'call', + 'profilePrinting', + ) # Tell pytype not to worry about dynamic attributes. -_HAS_DYNAMIC_ATTRIBUTES = True +_HAS_DYNAMIC_ATTRIBUTES = True # pylint: disable=unused-variable # A list of autogenerated class names added by _InitializeGeneratedClasses. _generatedClasses: list[str] = [] @@ -143,6 +147,7 @@ def __delattr__(self, name: Hashable) -> None: Blob, Classifier, Clusterer, + Color, ConfusionMatrix, Date, DateRange, @@ -159,6 +164,7 @@ def __delattr__(self, name: Hashable) -> None: List, Model, Number, + Palette, PixelType, Projection, Reducer, diff --git a/python/ee/_arg_types.py b/python/ee/_arg_types.py index 1fb62c60b..40d79c0f6 100644 --- a/python/ee/_arg_types.py +++ b/python/ee/_arg_types.py @@ -7,6 +7,7 @@ from ee import classifier from ee import clusterer +from ee import color from ee import computedobject from ee import confusionmatrix from ee import daterange @@ -23,6 +24,7 @@ from ee import image from ee import imagecollection from ee import kernel +from ee import palette from ee import projection from ee import reducer @@ -36,6 +38,7 @@ Bool = Union[bool, ee_number.Number, computedobject.ComputedObject] Classifier = Union[classifier.Classifier, computedobject.ComputedObject] Clusterer = Union[clusterer.Clusterer, computedobject.ComputedObject] +Color = Union[str, ee_list.List, color.Color, computedobject.ComputedObject] ConfusionMatrix = Union[ confusionmatrix.ConfusionMatrix, computedobject.ComputedObject ] @@ -79,6 +82,9 @@ computedobject.ComputedObject, ] Number = Union[float, ee_number.Number, computedobject.ComputedObject] +Palette = Union[ + str, ee_list.List, palette.Palette, computedobject.ComputedObject +] Projection = Union[ str, ee_string.String, diff --git a/python/ee/color.py b/python/ee/color.py new file mode 100644 index 000000000..dd196ae58 --- /dev/null +++ b/python/ee/color.py @@ -0,0 +1,219 @@ +"""A wrapper for colors.""" + +from __future__ import annotations + +from typing import Any + +from ee import _arg_types +from ee import apifunction +from ee import computedobject +from ee import ee_exception +from ee import ee_list + + +class Color(computedobject.ComputedObject): + """An object to represent colors.""" + + _initialized = False + + # Tell pytype to not complain about dynamic attributes. + _HAS_DYNAMIC_ATTRIBUTES = True + + def __init__(self, input: Any = None, color: Any = None): # pylint: disable=redefined-builtin + """Construct a color wrapper. + + This constructor accepts the following args: + 1) A W3C compatible color string. + 2) A list of RGBA values in the range of [0:1]. + 3) A ComputedObject returning a color. + + Args: + input: The color to wrap. + color: The color to wrap. (Deprecated, use 'input' instead.) + """ + self.initialize() + + color = input if input is not None else color + if color is None: + raise ee_exception.EEException( + 'Required argument (input) missing to function: Color' + ) + + if isinstance(color, computedobject.ComputedObject): + super().__init__(color.func, color.args, color.varName) + else: + super().__init__(apifunction.ApiFunction(self.name()), {'input': color}) + + @classmethod + def initialize(cls) -> None: + """Imports API functions to this class.""" + if not cls._initialized: + apifunction.ApiFunction.importApi(cls, cls.name(), cls.name()) + cls._initialized = True + + @classmethod + def reset(cls) -> None: + """Removes imported API functions from this class.""" + apifunction.ApiFunction.clearApi(cls) + cls._initialized = False + + @classmethod + def name(cls) -> str: + return 'Color' + + @classmethod + def fromHsv(cls, hsv: _arg_types.List) -> Color: + """Creates a Color given a list of HSV values. + + Args: + hsv: A list of HSV (hue, saturation, value) values in the range [0:1]. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.fromHsv', hsv) + + @classmethod + def fromHsl(cls, hsl: _arg_types.List) -> Color: + """Creates a Color given a list of HSL values. + + Args: + hsl: A list of HSL (hue, saturation, luminosity) values in the range + [0:1]. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.fromHsl', hsl) + + @classmethod + def fromLab(cls, lab: _arg_types.List) -> Color: + """Creates a Color given a list of CIE-LAB values. + + Args: + lab: A list of CIE-LAB values. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.fromLab', lab) + + @classmethod + def fromLch(cls, lch: _arg_types.List) -> Color: + """Creates a Color given a list of CIE-LCH values. + + Args: + lch: A list of CIE-LCH (lightness, chroma, hue) values. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.fromLch', lch) + + @classmethod + def gray( + cls, value: _arg_types.Number, alpha: _arg_types.Number = 1.0 + ) -> Color: + """Creates a gray color. + + Args: + value: The gray value in the range [0:1]. + alpha: The alpha value in the range [0:1]. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.gray', value, alpha) + + @classmethod + def mix( + cls, + start: Color, + end: Color, + ratio: _arg_types.Number = 0.5, + colorspace: _arg_types.String = 'rgb', + ) -> Color: + """Mixes two colors. + + Args: + start: The starting color. + end: The ending color. + ratio: The mix ratio. + colorspace: The colorspace to mix in. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_( + 'Color.mix', start, end, ratio, colorspace + ) + + def brighter(self, scale: _arg_types.Number = 0.7) -> Color: + """Scale each of the RGB channels to produce a brighter color. + + Args: + scale: The scale factor. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.brighter', self, scale) + + def darker(self, scale: _arg_types.Number = 0.7) -> Color: + """Scale each of the RGB channels to produce a darker color. + + Args: + scale: The scale factor. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Color.darker', self, scale) + + def toHsl(self) -> ee_list.List: + """Convert a color to HSL. + + Returns: + An ee.List. + """ + return apifunction.ApiFunction.call_('Color.toHsl', self) + + def toHsv(self) -> ee_list.List: + """Convert a color to HSV. + + Returns: + An ee.List. + """ + return apifunction.ApiFunction.call_('Color.toHsv', self) + + def toLab(self) -> ee_list.List: + """Convert a color to CIE-Lab. + + Returns: + An ee.List. + """ + return apifunction.ApiFunction.call_('Color.toLab', self) + + def toLch(self) -> ee_list.List: + """Convert a color to CIE-LCH. + + Returns: + An ee.List. + """ + return apifunction.ApiFunction.call_('Color.toLch', self) + + def toRgb(self) -> ee_list.List: + """Convert a color to RGB. + + Returns: + An ee.List. + """ + return apifunction.ApiFunction.call_('Color.toRGB', self) + + def toHexString(self) -> Any: + """Returns value of a color as an RGBA hex string. + + Returns: + An ee.String. + """ + return apifunction.ApiFunction.call_('Color.toHexString', self) diff --git a/python/ee/palette.py b/python/ee/palette.py new file mode 100644 index 000000000..efbc7bba4 --- /dev/null +++ b/python/ee/palette.py @@ -0,0 +1,222 @@ +"""A wrapper for palettes.""" + +from __future__ import annotations + +from typing import Any + +from ee import _arg_types +from ee import apifunction +from ee import color as ee_color +from ee import computedobject +from ee import ee_list + + +class Palette(computedobject.ComputedObject): + """An object to represent palettes.""" + + _initialized = False + + # Tell pytype to not complain about dynamic attributes. + _HAS_DYNAMIC_ATTRIBUTES = True + + def __init__( + self, + colors: Any = None, + mode: _arg_types.String | None = None, + min: _arg_types.Number | None = None, # pylint: disable=redefined-builtin + max: _arg_types.Number | None = None, # pylint: disable=redefined-builtin + padding: _arg_types.List | None = None, + classes: _arg_types.List | None = None, + positions: _arg_types.List | None = None, + correctLightness: _arg_types.Bool | None = None, # pylint: disable=invalid-name + gamma: _arg_types.Number | None = None, + bezier: _arg_types.Bool | None = None, + ): + """Construct a palette wrapper. + + Args: + colors: A list of colors or the name of a predefined color palette. + mode: The colorspace in which to interpolate. + min: The minimum value of the palette. + max: The maximum value of the palette. + padding: Shifts the color range by padding the end of the color scale. + classes: Create a palette representing discrete classes. + positions: Set the positions for the colors. + correctLightness: Correct the color spacing to spread lightness range. + gamma: A gamma correction for the palette. + bezier: Sets the palette to use Bezier interpolation. + """ + self.initialize() + + if isinstance(colors, computedobject.ComputedObject) and all( + v is None + for v in [ + mode, + min, + max, + padding, + classes, + positions, + correctLightness, + gamma, + bezier, + ] + ): + super().__init__(colors.func, colors.args, colors.varName) + else: + super().__init__( + apifunction.ApiFunction(self.name()), + { + 'colors': colors, + 'mode': mode if mode is not None else 'RGB', + 'min': min if min is not None else 0.0, + 'max': max if max is not None else 1.0, + 'padding': padding, + 'classes': classes, + 'positions': positions, + 'correctLightness': ( + correctLightness if correctLightness is not None else False + ), + 'gamma': gamma if gamma is not None else 1.0, + 'bezier': bezier if bezier is not None else False, + }, + ) + + @classmethod + def initialize(cls) -> None: + """Imports API functions to this class.""" + if not cls._initialized: + apifunction.ApiFunction.importApi(cls, cls.name(), cls.name()) + cls._initialized = True + + @classmethod + def reset(cls) -> None: + """Removes imported API functions from this class.""" + apifunction.ApiFunction.clearApi(cls) + cls._initialized = False + + @classmethod + def name(cls) -> str: + return 'Palette' + + def getColor(self, value: _arg_types.Number) -> ee_color.Color: + """Returns the color at the given value. + + Args: + value: The value to look up. + + Returns: + An ee.Color. + """ + return apifunction.ApiFunction.call_('Palette.getColor', self, value) + + def getColors( + self, nColors: _arg_types.Integer = 0 # pylint: disable=invalid-name + ) -> ee_list.List: + """Get colors from this palette. + + Args: + nColors: The number of equally spaced colors to retrieve. + + Returns: + An ee.List of ee.Colors. + """ + return apifunction.ApiFunction.call_('Palette.getColors', self, nColors) + + def mode(self, mode: _arg_types.String) -> Palette: + """Set the colorspace interpolation mode. + + Args: + mode: The colorspace mode. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.mode', self, mode) + + def limits( + self, + min: _arg_types.Number, # pylint: disable=redefined-builtin + max: _arg_types.Number, # pylint: disable=redefined-builtin + ) -> Palette: + """Set the minimum and maximum limits for the palette. + + Args: + min: The minimum value. + max: The maximum value. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.limits', self, min, max) + + def positions(self, positions: _arg_types.List) -> Palette: + """Set the position for each color in the palette. + + Args: + positions: A list of values specifying the position for each color. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.positions', self, positions) + + def classes(self, classes: Any) -> Palette: + """Use discrete classes as opposed to a continuous gradient. + + Args: + classes: Either a list of class break values, or a single number. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.classes', self, classes) + + def padding( + self, left: _arg_types.Number, right: _arg_types.Number = None + ) -> Palette: + """Shifts the color range by padding the end of the color scale. + + Args: + left: The left padding. + right: The right padding. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.padding', self, left, right) + + def gamma(self, gamma: _arg_types.Number) -> Palette: + """Apply a gamma correction to the palette. + + Args: + gamma: The gamma value. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.gamma', self, gamma) + + def bezier(self, interpolate: _arg_types.Bool = True) -> Palette: + """Sets the palette to use bezier interpolation. + + Args: + interpolate: Whether to use bezier interpolation. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_('Palette.bezier', self, interpolate) + + def correctLightness(self, correct: _arg_types.Bool = True) -> Palette: + """Correct the color spacing to spread lightness range evenly. + + Args: + correct: Whether to correct lightness. + + Returns: + An ee.Palette. + """ + return apifunction.ApiFunction.call_( + 'Palette.correctLightness', self, correct + ) diff --git a/python/ee/tests/algorithms.json b/python/ee/tests/algorithms.json index bed68d79b..cfe9a5c50 100644 --- a/python/ee/tests/algorithms.json +++ b/python/ee/tests/algorithms.json @@ -645,6 +645,25 @@ "description": "List of spike removal magnitude values to be used for each band. Spikes with a magnitude above this value are removed. If not provided, the spikeRemovalMagnitude value will be used for all bands.", "optional": true, "defaultValue": [] + }, { + "argumentName": "maxErrorList", + "type": "List\u003cFloat\u003e", + "description": "List of maximum error (RMSE) values to be used for each band. If not provided, the maxError value will be used for all bands.", + "optional": true, + "defaultValue": [] + }, { + "argumentName": "spikesToleranceList", + "type": "List\u003cFloat\u003e", + "description": "List of spike tolerance values to be used for each band. A value of 1 indicates no spike removal. If not provided, the spikesTolerance value will be used for all bands.", + "optional": true, + "defaultValue": [] + }, { + "argumentName": "spikeRemovalMagnitudeList", + "type": "List\u003cFloat\u003e", + "description": "List of spike removal magnitude values to be used for each band. Spikes with a magnitude above this value are removed. If not provided, the spikeRemovalMagnitude value will be used for all bands.", + "description": "Maximum number of segments permitted in the fitted tajectory.", + "optional": true, + "defaultValue": [] }, { "argumentName": "maxError", "type": "Float", @@ -3734,6 +3753,563 @@ "argumentName": "clusterer", "type": "Clusterer" }] + }, { + "name": "algorithms/Color", + "description": "Creates a Color.", + "returnType": "Color", + "arguments": [{ + "argumentName": "input", + "type": "Object", + "description": "Either a W3C compatible color string or a list of RGBA values in the range of [0:1]." + }] + }, { + "name": "algorithms/Color.brighter", + "description": "Scale each of the RGB channels to produce a brighter color.", + "returnType": "Color", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }, { + "argumentName": "scale", + "type": "Float", + "optional": true, + "defaultValue": 0.7 + }] + }, { + "name": "algorithms/Color.darker", + "description": "Scale each of the RGB channels to produce a darker color.", + "returnType": "Color", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }, { + "argumentName": "scale", + "type": "Float", + "optional": true, + "defaultValue": 0.7 + }] + }, { + "name": "algorithms/Color.fromHsl", + "description": "Creates a Color given a list of HSL (hue, saturation, luminosity) values and an optional alpha value, all in the range of [0:1].", + "returnType": "Color", + "arguments": [{ + "argumentName": "hsl", + "type": "List\u003cFloat\u003e" + }] + }, { + "name": "algorithms/Color.fromHsv", + "description": "Creates a Color given a list of HSV (hue, saturation, value) values and an optional alpha value, all in the range of [0:1].", + "returnType": "Color", + "arguments": [{ + "argumentName": "hsv", + "type": "List\u003cFloat\u003e" + }] + }, { + "name": "algorithms/Color.fromLab", + "description": "Creates a Color given a list of CIE-LAB values and an optional alpha value. The components are unnormalized and have the approximate ranges of: L\u003d[0:100], a,b\u003d[-127:128].", + "returnType": "Color", + "arguments": [{ + "argumentName": "lab", + "type": "List\u003cFloat\u003e" + }] + }, { + "name": "algorithms/Color.fromLch", + "description": "Creates a Color given a list of CIE-LCH (lightness, chroma, hue) values and an optional alpha value. The components are unnormalized and have an approximate range of L\u003d[0:100], C\u003d[0:180], H\u003d[0:360] (degrees).", + "returnType": "Color", + "arguments": [{ + "argumentName": "lch", + "type": "List\u003cFloat\u003e" + }] + }, { + "name": "algorithms/Color.gray", + "description": "Creates a color with equal R, G and B values.", + "returnType": "Color", + "arguments": [{ + "argumentName": "value", + "type": "Float" + }, { + "argumentName": "alpha", + "type": "Float", + "optional": true, + "defaultValue": 1.0 + }] + }, { + "name": "algorithms/Color.mix", + "description": "Mixes two colors by the given ratio in the specified colorspace.", + "returnType": "Color", + "arguments": [{ + "argumentName": "start", + "type": "Color" + }, { + "argumentName": "end", + "type": "Color" + }, { + "argumentName": "ratio", + "type": "Float", + "optional": true, + "defaultValue": 0.5 + }, { + "argumentName": "colorspace", + "type": "String", + "optional": true, + "defaultValue": "rgb" + }] + }, { + "name": "algorithms/Color.toHexString", + "description": "Returns value of a color as an RGBA hex string.", + "returnType": "String", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Color.toHsl", + "description": "Convert a color to HSL and returns the values and alpha channel as a list of doubles in the range [0:1].", + "returnType": "List\u003cFloat\u003e", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Color.toHsv", + "description": "Convert a color to HSV and returns the values and alpha channel as a list of doubles in the range [0:1].", + "returnType": "List\u003cFloat\u003e", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Color.toLab", + "description": "Convert a color to CIE-Lab and returns the values and alpha channel as a list of doubles. The components are unnormalized and have the approximate ranges of: L\u003d[0:100], a,b\u003d[-127:128].", + "returnType": "List\u003cFloat\u003e", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Color.toLch", + "description": "Convert a color to CIE-LCH and returns the values and alpha channel as a list of doubles. The components are unnormalized and have an approximate range of L\u003d[0:100], C\u003d[0:180], H\u003d[0:360] (degrees).", + "returnType": "List\u003cFloat\u003e", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Color.toRGB", + "description": "Returns the RGB and alpha value as a list of numbers in the range of [0:1].", + "returnType": "List\u003cFloat\u003e", + "arguments": [{ + "argumentName": "color", + "type": "Color" + }] + }, { + "name": "algorithms/Palette", + "description": "Creates a palette.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "colors", + "type": "Object", + "description": "A list of colors or the name of a predefined color palette.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "mode", + "type": "String", + "description": "The colorspace in which to interpolate. One of \u0027rgb\u0027, \u0027lrgb\u0027, \u0027hsv\u0027, \u0027hsl\u0027, \u0027lab\u0027 or \u0027lch\u0027.", + "optional": true, + "defaultValue": "RGB" + }, { + "argumentName": "min", + "type": "Float", + "description": "The minimum value of the palette. Values less than min will be clamped to min.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "max", + "type": "Float", + "description": "The maximum value of the palette. Values greater than max will be clamped to max.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "padding", + "type": "List\u003cFloat\u003e", + "description": "Shifts the color range by padding the end of the color scale (a percentage 0-1). Positive values reduce the spread of the gradient and negative values expand it. If only 1 value is provided, the same padding will be applied to both ends.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "classes", + "type": "List\u003cFloat\u003e", + "description": "Create a palette representing discrete classes instead of a continuous gradient. Either a list of class break values, or a single number indicating the number of equidistant breaks to generate between \u0027min\u0027 and \u0027max\u0027. This option is mutually exclusive with \u0027positions\u0027.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "positions", + "type": "List\u003cFloat\u003e", + "description": "Set the positions for the colors. The number of positions must exactly match the number of colors in the palette. Resets \u0027min\u0027 \u0026 \u0027max\u0027 to first and last value. This option is mutually exclusive with \u0027min\u0027/\u0027max\u0027 and \u0027classes\u0027.", + "optional": true, + "defaultValue": null + }, { + "argumentName": "correctLightness", + "type": "Boolean", + "description": "Correct the color spacing to spread the (Lab) lightness range evenly over the range of values.", + "optional": true, + "defaultValue": false + }, { + "argumentName": "gamma", + "type": "Float", + "description": "A gamma correction for the palette. Numbers greater than 1 increase lightness.", + "optional": true, + "defaultValue": 1.0 + }, { + "argumentName": "bezier", + "type": "Boolean", + "description": "Sets the palette to use Bezier interpolation.", + "optional": true, + "defaultValue": false + }] + }, { + "name": "algorithms/Palette.bezier", + "description": "Sets the palette to use bezier interpolation.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "interpolate", + "type": "Boolean", + "optional": true, + "defaultValue": true + }] + }, { + "name": "algorithms/Palette.classes", + "description": "Use discrete classes as opposed to a continuous gradient.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "classes", + "type": "Object", + "description": "Either a list of class break values, or a single number indicating the number of equidistant breaks to generate. This overrides any values set by positions and if a list of break values is given, explicitly sets min/max to the first/last value." + }] + }, { + "name": "algorithms/Palette.correctLightness", + "description": "Correct the color spacing to spread the (Lab) lightness range evenly over the domain.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "correct", + "type": "Boolean", + "optional": true, + "defaultValue": true + }] + }, { + "name": "algorithms/Palette.gamma", + "description": "Apply a gamma correction to the palette.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "gamma", + "type": "Float" + }] + }, { + "name": "algorithms/Palette.getColor", + "description": "Returns the color at the given value, interpolated according to the mode.", + "returnType": "Color", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "value", + "type": "Float" + }] + }, { + "name": "algorithms/Palette.getColors", + "description": "Get colors from this palette.", + "returnType": "List\u003cColor\u003e", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "nColors", + "type": "Integer", + "description": "The number of equally spaced colors to retrieve from the palette. If nColors is 0 or unspecified, the algorithm returns the entire list of colors used to construct the palette.", + "optional": true, + "defaultValue": 0.0 + }] + }, { + "name": "algorithms/Palette.limits", + "description": "Set the minimum and maximum limits for the palette. The palette colors will be evenly spaced between these values, overriding any previous settings for \u0027positions\u0027 or \u0027classes\u0027.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "min", + "type": "Float", + "description": "The minimum value of the palette. Values less than min will be clamped to min." + }, { + "argumentName": "max", + "type": "Float", + "description": "The maximum value of the palette. Values greater than max will be clamped to max." + }] + }, { + "name": "algorithms/Palette.mode", + "description": "Set the colorspace interpolation mode. One of \u0027rgb\u0027, \u0027lrgb\u0027, \u0027hsv\u0027, \u0027hsl\u0027, \u0027lab\u0027, or \u0027lch\u0027. The palette will mix colors in this color space, using EEColor.mix.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "mode", + "type": "String" + }] + }, { + "name": "algorithms/Palette.padding", + "description": "Shifts the color range by padding the end of the color scale. Positive values reduce the spread of the gradient and negative values expand it. If only 1 value is provided, the same padding will be applied to both ends.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "left", + "type": "Float" + }, { + "argumentName": "right", + "type": "Float", + "optional": true, + "defaultValue": null + }] + }, { + "name": "algorithms/Palette.positions", + "description": "Set the position for each color in the palette.", + "returnType": "Palette", + "arguments": [{ + "argumentName": "palette", + "type": "Palette" + }, { + "argumentName": "positions", + "type": "List\u003cFloat\u003e", + "description": "A list of values specifying the position for each color in the palette. This overrides any value of \u0027classes\u0027, and explicitly sets min/max to the first/last position." + }] + }, { + "name": "algorithms/Palette.OrRd", + "description": "Returns the Colorbrewer named palette: \u0027OrRd\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PuBu", + "description": "Returns the Colorbrewer named palette: \u0027PuBu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.BuPu", + "description": "Returns the Colorbrewer named palette: \u0027BuPu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Oranges", + "description": "Returns the Colorbrewer named palette: \u0027Oranges\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.BuGn", + "description": "Returns the Colorbrewer named palette: \u0027BuGn\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.YlOrBr", + "description": "Returns the Colorbrewer named palette: \u0027YlOrBr\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.YlGn", + "description": "Returns the Colorbrewer named palette: \u0027YlGn\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Reds", + "description": "Returns the Colorbrewer named palette: \u0027Reds\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.RdPu", + "description": "Returns the Colorbrewer named palette: \u0027RdPu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Greens", + "description": "Returns the Colorbrewer named palette: \u0027Greens\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.YlGnBu", + "description": "Returns the Colorbrewer named palette: \u0027YlGnBu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Purples", + "description": "Returns the Colorbrewer named palette: \u0027Purples\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.GnBu", + "description": "Returns the Colorbrewer named palette: \u0027GnBu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Greys", + "description": "Returns the Colorbrewer named palette: \u0027Greys\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.YlOrRd", + "description": "Returns the Colorbrewer named palette: \u0027YlOrRd\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PuRd", + "description": "Returns the Colorbrewer named palette: \u0027PuRd\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Blues", + "description": "Returns the Colorbrewer named palette: \u0027Blues\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PuBuGn", + "description": "Returns the Colorbrewer named palette: \u0027PuBuGn\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Spectral", + "description": "Returns the Colorbrewer named palette: \u0027Spectral\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.RdYlGn", + "description": "Returns the Colorbrewer named palette: \u0027RdYlGn\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.RdBu", + "description": "Returns the Colorbrewer named palette: \u0027RdBu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PiYG", + "description": "Returns the Colorbrewer named palette: \u0027PiYG\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PRGn", + "description": "Returns the Colorbrewer named palette: \u0027PRGn\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.RdYlBu", + "description": "Returns the Colorbrewer named palette: \u0027RdYlBu\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.BrBG", + "description": "Returns the Colorbrewer named palette: \u0027BrBG\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.RdGy", + "description": "Returns the Colorbrewer named palette: \u0027RdGy\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.PuOr", + "description": "Returns the Colorbrewer named palette: \u0027PuOr\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Accent", + "description": "Returns the Colorbrewer named palette: \u0027Accent\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Set1", + "description": "Returns the Colorbrewer named palette: \u0027Set1\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Set2", + "description": "Returns the Colorbrewer named palette: \u0027Set2\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Set3", + "description": "Returns the Colorbrewer named palette: \u0027Set3\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Dark2", + "description": "Returns the Colorbrewer named palette: \u0027Dark2\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Paired", + "description": "Returns the Colorbrewer named palette: \u0027Paired\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Pastel2", + "description": "Returns the Colorbrewer named palette: \u0027Pastel2\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Pastel1", + "description": "Returns the Colorbrewer named palette: \u0027Pastel1\u0027. See https://colorbrewer2.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Viridis", + "description": "Returns the Colorbrewer named palette: \u0027Viridis\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Magma", + "description": "Returns the Colorbrewer named palette: \u0027Magma\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Inferno", + "description": "Returns the Colorbrewer named palette: \u0027Inferno\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Plasma", + "description": "Returns the Colorbrewer named palette: \u0027Plasma\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Cool", + "description": "Returns the Colorbrewer named palette: \u0027Cool\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Hot", + "description": "Returns the Colorbrewer named palette: \u0027Hot\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.Copper", + "description": "Returns the Colorbrewer named palette: \u0027Copper\u0027. See https://matplotlib.org for more information.", + "returnType": "Palette" + }, { + "name": "algorithms/Palette.cubeHelix", + "description": "Creates a palette using cubehelix interpolation. See: D.A. Green \u0027A colour scheme for the display of astronomical intensity images\u0027 (http://astron-soc.in/bulletin/11June/289392011.pdf)", + "returnType": "Palette", + "arguments": [{ + "argumentName": "startHue", + "type": "Float", + "description": "The start hue, normalized [0:1].", + "optional": true, + "defaultValue": 0.8 + }, { + "argumentName": "rotations", + "type": "Float", + "description": "The number of rotations through the hue space. Negative values correspond to Blue-\u003eRed direction.", + "optional": true, + "defaultValue": -1.5 + }, { + "argumentName": "saturation", + "type": "Float", + "description": "The saturation scaling factor.", + "optional": true, + "defaultValue": 1.0 + }, { + "argumentName": "gamma", + "type": "Float", + "description": "A gamma correction factor. Values less than 1 emphasize low intensity values.", + "optional": true, + "defaultValue": 1.0 + }, { + "argumentName": "startLightness", + "type": "Float", + "description": "Controls how light the colors are. Lightness is linearly interpolated between startLightness and endLightness.", + "optional": true, + "defaultValue": 0.0 + }, { + "argumentName": "endLightness", + "type": "Float", + "optional": true, + "defaultValue": 1.0 + }, { + "argumentName": "endHue", + "type": "Float", + "description": "The ending hue. If specified, the distance between (startHue + rotations) and endHue will be added to rotations to ensure the last color has a hue of endHue.", + "optional": true, + "defaultValue": null + }] }, { "name": "algorithms/Geometry.fromS2CellId", "description": "Constructs the Polygon corresponding to an S2 cell id.", @@ -16654,4 +17230,4 @@ }], "hidden": true }] -} \ No newline at end of file +} diff --git a/python/ee/tests/color_test.py b/python/ee/tests/color_test.py new file mode 100644 index 000000000..2721ce030 --- /dev/null +++ b/python/ee/tests/color_test.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +"""Tests for the ee.Color class.""" + +import unittest +import ee +from ee import apitestcase + + +class ColorTest(apitestcase.ApiTestCase): + + def test_constructors(self): + """Test the ee.Color constructors.""" + self.assertEqual(ee.ApiFunction('Color'), ee.Color('red').func) + self.assertEqual({'input': 'red'}, ee.Color('red').args) + + self.assertEqual(ee.ApiFunction('Color'), ee.Color([1, 0, 0]).func) + self.assertEqual({'input': [1, 0, 0]}, ee.Color([1, 0, 0]).args) + + def test_static_methods(self): + """Test the ee.Color static methods.""" + self.assertEqual( + ee.ApiFunction('Color.fromHsv'), ee.Color.fromHsv([0, 1, 1]).func + ) + self.assertEqual( + ee.ApiFunction('Color.fromHsl'), ee.Color.fromHsl([0, 1, 0.5]).func + ) + self.assertEqual( + ee.ApiFunction('Color.fromLab'), ee.Color.fromLab([50, 0, 0]).func + ) + self.assertEqual( + ee.ApiFunction('Color.fromLch'), ee.Color.fromLch([50, 0, 0]).func + ) + self.assertEqual(ee.ApiFunction('Color.gray'), ee.Color.gray(0.5).func) + self.assertEqual( + ee.ApiFunction('Color.mix'), + ee.Color.mix(ee.Color('red'), ee.Color('blue')).func, + ) + + def test_instance_methods(self): + """Test the ee.Color instance methods.""" + color = ee.Color('red') + self.assertEqual(ee.ApiFunction('Color.brighter'), color.brighter().func) + self.assertEqual(ee.ApiFunction('Color.darker'), color.darker().func) + self.assertEqual(ee.ApiFunction('Color.toHsl'), color.toHsl().func) + self.assertEqual(ee.ApiFunction('Color.toHsv'), color.toHsv().func) + self.assertEqual(ee.ApiFunction('Color.toLab'), color.toLab().func) + self.assertEqual(ee.ApiFunction('Color.toLch'), color.toLch().func) + self.assertEqual(ee.ApiFunction('Color.toRGB'), color.toRgb().func) + self.assertEqual( + ee.ApiFunction('Color.toHexString'), color.toHexString().func + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/ee/tests/palette_test.py b/python/ee/tests/palette_test.py new file mode 100644 index 000000000..5fbe517bd --- /dev/null +++ b/python/ee/tests/palette_test.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +"""Tests for the ee.Palette class.""" + +import unittest +import ee +from ee import apitestcase + + +class PaletteTest(apitestcase.ApiTestCase): + + def test_constructors(self): + """Test the ee.Palette constructors.""" + self.assertEqual(ee.ApiFunction('Palette'), ee.Palette('spectral').func) + self.assertEqual( + { + 'colors': 'spectral', + 'mode': 'RGB', + 'min': 0.0, + 'max': 1.0, + 'padding': None, + 'classes': None, + 'positions': None, + 'correctLightness': False, + 'gamma': 1.0, + 'bezier': False, + }, + ee.Palette('spectral').args, + ) + + self.assertEqual( + ee.ApiFunction('Palette'), ee.Palette(['red', 'blue']).func + ) + self.assertEqual( + { + 'colors': ['red', 'blue'], + 'mode': 'RGB', + 'min': 0.0, + 'max': 1.0, + 'padding': None, + 'classes': None, + 'positions': None, + 'correctLightness': False, + 'gamma': 1.0, + 'bezier': False, + }, + ee.Palette(['red', 'blue']).args, + ) + + def test_instance_methods(self): + """Test the ee.Palette instance methods.""" + palette = ee.Palette('spectral') + self.assertEqual( + ee.ApiFunction('Palette.getColor'), palette.getColor(0.5).func + ) + self.assertEqual( + ee.ApiFunction('Palette.getColors'), palette.getColors(10).func + ) + self.assertEqual(ee.ApiFunction('Palette.mode'), palette.mode('HSL').func) + self.assertEqual( + ee.ApiFunction('Palette.limits'), palette.limits(0, 100).func + ) + self.assertEqual( + ee.ApiFunction('Palette.positions'), + palette.positions([0, 10, 100]).func, + ) + self.assertEqual(ee.ApiFunction('Palette.classes'), palette.classes(5).func) + self.assertEqual( + ee.ApiFunction('Palette.padding'), palette.padding(0.1, 0.1).func + ) + self.assertEqual(ee.ApiFunction('Palette.gamma'), palette.gamma(2.2).func) + self.assertEqual( + ee.ApiFunction('Palette.bezier'), palette.bezier(True).func + ) + self.assertEqual( + ee.ApiFunction('Palette.correctLightness'), + palette.correctLightness(True).func, + ) + + +if __name__ == '__main__': + unittest.main()