Merge pull request #7637 from marcjansen/named-exports-color

Named exports from the ol/color module
This commit is contained in:
Marc Jansen
2017-12-19 17:11:03 +01:00
committed by GitHub
11 changed files with 95 additions and 108 deletions

View File

@@ -3,7 +3,6 @@
*/
import {assert} from './asserts.js';
import _ol_math_ from './math.js';
var _ol_color_ = {};
/**
@@ -12,7 +11,7 @@ var _ol_color_ = {};
* @type {RegExp}
* @private
*/
_ol_color_.HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3,4}){1,2}$/i;
var HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3,4}){1,2}$/i;
/**
@@ -21,23 +20,7 @@ _ol_color_.HEX_COLOR_RE_ = /^#(?:[0-9a-f]{3,4}){1,2}$/i;
* @type {RegExp}
* @private
*/
_ol_color_.NAMED_COLOR_RE_ = /^([a-z]*)$/i;
/**
* Return the color as an array. This function maintains a cache of calculated
* arrays which means the result should not be modified.
* @param {ol.Color|string} color Color.
* @return {ol.Color} Color.
* @api
*/
_ol_color_.asArray = function(color) {
if (Array.isArray(color)) {
return color;
} else {
return _ol_color_.fromString(/** @type {string} */ (color));
}
};
var NAMED_COLOR_RE_ = /^([a-z]*)$/i;
/**
@@ -46,34 +29,34 @@ _ol_color_.asArray = function(color) {
* @return {string} Rgba string.
* @api
*/
_ol_color_.asString = function(color) {
export function asString(color) {
if (typeof color === 'string') {
return color;
} else {
return _ol_color_.toString(color);
return toString(color);
}
};
}
/**
* Return named color as an rgba string.
* @param {string} color Named color.
* @return {string} Rgb string.
*/
_ol_color_.fromNamed = function(color) {
function fromNamed(color) {
var el = document.createElement('div');
el.style.color = color;
document.body.appendChild(el);
var rgb = getComputedStyle(el).color;
document.body.removeChild(el);
return rgb;
};
}
/**
* @param {string} s String.
* @return {ol.Color} Color.
*/
_ol_color_.fromString = (
export var fromString = (
function() {
// We maintain a small cache of parsed strings. To provide cheap LRU-like
@@ -116,7 +99,7 @@ _ol_color_.fromString = (
}
}
}
color = _ol_color_.fromStringInternal_(s);
color = fromStringInternal_(s);
cache[s] = color;
++cacheSize;
}
@@ -126,20 +109,34 @@ _ol_color_.fromString = (
})();
/**
* Return the color as an array. This function maintains a cache of calculated
* arrays which means the result should not be modified.
* @param {ol.Color|string} color Color.
* @return {ol.Color} Color.
* @api
*/
export function asArray(color) {
if (Array.isArray(color)) {
return color;
} else {
return fromString(/** @type {string} */ (color));
}
}
/**
* @param {string} s String.
* @private
* @return {ol.Color} Color.
*/
_ol_color_.fromStringInternal_ = function(s) {
function fromStringInternal_(s) {
var r, g, b, a, color, parts;
if (_ol_color_.NAMED_COLOR_RE_.exec(s)) {
s = _ol_color_.fromNamed(s);
if (NAMED_COLOR_RE_.exec(s)) {
s = fromNamed(s);
}
if (_ol_color_.HEX_COLOR_RE_.exec(s)) { // hex
if (HEX_COLOR_RE_.exec(s)) { // hex
var n = s.length - 1; // number of hex digits
var d; // number of digits per channel
if (n <= 4) {
@@ -167,38 +164,39 @@ _ol_color_.fromStringInternal_ = function(s) {
color = [r, g, b, a / 255];
} else if (s.indexOf('rgba(') == 0) { // rgba()
parts = s.slice(5, -1).split(',').map(Number);
color = _ol_color_.normalize(parts);
color = normalize(parts);
} else if (s.indexOf('rgb(') == 0) { // rgb()
parts = s.slice(4, -1).split(',').map(Number);
parts.push(1);
color = _ol_color_.normalize(parts);
color = normalize(parts);
} else {
assert(false, 14); // Invalid color
}
return /** @type {ol.Color} */ (color);
};
}
/**
* TODO this function is only used in the test, we probably shouldn't export it
* @param {ol.Color} color Color.
* @param {ol.Color=} opt_color Color.
* @return {ol.Color} Clamped color.
*/
_ol_color_.normalize = function(color, opt_color) {
export function normalize(color, opt_color) {
var result = opt_color || [];
result[0] = _ol_math_.clamp((color[0] + 0.5) | 0, 0, 255);
result[1] = _ol_math_.clamp((color[1] + 0.5) | 0, 0, 255);
result[2] = _ol_math_.clamp((color[2] + 0.5) | 0, 0, 255);
result[3] = _ol_math_.clamp(color[3], 0, 1);
return result;
};
}
/**
* @param {ol.Color} color Color.
* @return {string} String.
*/
_ol_color_.toString = function(color) {
export function toString(color) {
var r = color[0];
if (r != (r | 0)) {
r = (r + 0.5) | 0;
@@ -213,5 +211,4 @@ _ol_color_.toString = function(color) {
}
var a = color[3] === undefined ? 1 : color[3];
return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
};
export default _ol_color_;
}

View File

@@ -1,7 +1,7 @@
/**
* @module ol/colorlike
*/
import _ol_color_ from './color.js';
import {asString} from './color.js';
/**
@@ -13,7 +13,7 @@ export function asColorLike(color) {
if (isColorLike(color)) {
return /** @type {string|CanvasPattern|CanvasGradient} */ (color);
} else {
return _ol_color_.asString(/** @type {ol.Color} */ (color));
return asString(/** @type {ol.Color} */ (color));
}
}

View File

@@ -5,7 +5,7 @@ import {inherits} from '../index.js';
import _ol_Feature_ from '../Feature.js';
import _ol_array_ from '../array.js';
import {assert} from '../asserts.js';
import _ol_color_ from '../color.js';
import {asArray} from '../color.js';
import FeatureFormat from '../format/Feature.js';
import XMLFeature from '../format/XMLFeature.js';
import XSD from '../format/XSD.js';
@@ -2090,7 +2090,7 @@ KML.prototype.readProjection;
* @private
*/
KML.writeColorTextNode_ = function(node, color) {
var rgba = _ol_color_.asArray(color);
var rgba = asArray(color);
var opacity = (rgba.length == 4) ? rgba[3] : 1;
var abgr = [opacity * 255, rgba[2], rgba[1], rgba[0]];
var i;

View File

@@ -2,7 +2,7 @@
* @module ol/render/canvas/PolygonReplay
*/
import {inherits} from '../../index.js';
import _ol_color_ from '../../color.js';
import {asString} from '../../color.js';
import _ol_geom_flat_simplify_ from '../../geom/flat/simplify.js';
import _ol_render_canvas_ from '../canvas.js';
import _ol_render_canvas_Instruction_ from '../canvas/Instruction.js';
@@ -91,7 +91,7 @@ _ol_render_canvas_PolygonReplay_.prototype.drawCircle = function(circleGeometry,
// always fill the circle for hit detection
this.hitDetectionInstructions.push([
_ol_render_canvas_Instruction_.SET_FILL_STYLE,
_ol_color_.asString(_ol_render_canvas_.defaultFillStyle)
asString(_ol_render_canvas_.defaultFillStyle)
]);
if (state.strokeStyle !== undefined) {
this.hitDetectionInstructions.push([
@@ -133,7 +133,7 @@ _ol_render_canvas_PolygonReplay_.prototype.drawPolygon = function(polygonGeometr
// always fill the polygon for hit detection
this.hitDetectionInstructions.push([
_ol_render_canvas_Instruction_.SET_FILL_STYLE,
_ol_color_.asString(_ol_render_canvas_.defaultFillStyle)]
asString(_ol_render_canvas_.defaultFillStyle)]
);
if (state.strokeStyle !== undefined) {
this.hitDetectionInstructions.push([
@@ -165,7 +165,7 @@ _ol_render_canvas_PolygonReplay_.prototype.drawMultiPolygon = function(multiPoly
// always fill the multi-polygon for hit detection
this.hitDetectionInstructions.push([
_ol_render_canvas_Instruction_.SET_FILL_STYLE,
_ol_color_.asString(_ol_render_canvas_.defaultFillStyle)
asString(_ol_render_canvas_.defaultFillStyle)
]);
if (state.strokeStyle !== undefined) {
this.hitDetectionInstructions.push([

View File

@@ -3,7 +3,7 @@
*/
import {getUid, inherits} from '../../index.js';
import _ol_array_ from '../../array.js';
import _ol_color_ from '../../color.js';
import {asArray} from '../../color.js';
import {intersects} from '../../extent.js';
import _ol_obj_ from '../../obj.js';
import _ol_geom_flat_transform_ from '../../geom/flat/transform.js';
@@ -387,7 +387,7 @@ _ol_render_webgl_CircleReplay_.prototype.setFillStrokeStyle = function(fillStyle
strokeStyleColor = strokeStyle.getColor();
if (!(strokeStyleColor instanceof CanvasGradient) &&
!(strokeStyleColor instanceof CanvasPattern)) {
strokeStyleColor = _ol_color_.asArray(strokeStyleColor).map(function(c, i) {
strokeStyleColor = asArray(strokeStyleColor).map(function(c, i) {
return i != 3 ? c / 255 : c;
}) || _ol_render_webgl_.defaultStrokeStyle;
} else {
@@ -403,7 +403,7 @@ _ol_render_webgl_CircleReplay_.prototype.setFillStrokeStyle = function(fillStyle
var fillStyleColor = fillStyle ? fillStyle.getColor() : [0, 0, 0, 0];
if (!(fillStyleColor instanceof CanvasGradient) &&
!(fillStyleColor instanceof CanvasPattern)) {
fillStyleColor = _ol_color_.asArray(fillStyleColor).map(function(c, i) {
fillStyleColor = asArray(fillStyleColor).map(function(c, i) {
return i != 3 ? c / 255 : c;
}) || _ol_render_webgl_.defaultFillStyle;
} else {

View File

@@ -3,7 +3,7 @@
*/
import {getUid, inherits} from '../../index.js';
import _ol_array_ from '../../array.js';
import _ol_color_ from '../../color.js';
import {asArray} from '../../color.js';
import {intersects} from '../../extent.js';
import _ol_geom_flat_orient_ from '../../geom/flat/orient.js';
import _ol_geom_flat_transform_ from '../../geom/flat/transform.js';
@@ -647,7 +647,7 @@ _ol_render_webgl_LineStringReplay_.prototype.setFillStrokeStyle = function(fillS
var strokeStyleColor = strokeStyle.getColor();
if (!(strokeStyleColor instanceof CanvasGradient) &&
!(strokeStyleColor instanceof CanvasPattern)) {
strokeStyleColor = _ol_color_.asArray(strokeStyleColor).map(function(c, i) {
strokeStyleColor = asArray(strokeStyleColor).map(function(c, i) {
return i != 3 ? c / 255 : c;
}) || _ol_render_webgl_.defaultStrokeStyle;
} else {

View File

@@ -3,7 +3,7 @@
*/
import {getUid, inherits} from '../../index.js';
import _ol_array_ from '../../array.js';
import _ol_color_ from '../../color.js';
import {asArray} from '../../color.js';
import {intersects} from '../../extent.js';
import _ol_obj_ from '../../obj.js';
import _ol_geom_flat_contains_ from '../../geom/flat/contains.js';
@@ -1048,7 +1048,7 @@ _ol_render_webgl_PolygonReplay_.prototype.setFillStrokeStyle = function(fillStyl
var fillStyleColor = fillStyle ? fillStyle.getColor() : [0, 0, 0, 0];
if (!(fillStyleColor instanceof CanvasGradient) &&
!(fillStyleColor instanceof CanvasPattern)) {
fillStyleColor = _ol_color_.asArray(fillStyleColor).map(function(c, i) {
fillStyleColor = asArray(fillStyleColor).map(function(c, i) {
return i != 3 ? c / 255 : c;
}) || _ol_render_webgl_.defaultFillStyle;
} else {

View File

@@ -2,7 +2,7 @@
* @module ol/style/Fill
*/
import {getUid} from '../index.js';
import _ol_color_ from '../color.js';
import {asString} from '../color.js';
/**
* @classdesc
@@ -76,8 +76,7 @@ _ol_style_Fill_.prototype.getChecksum = function() {
) {
this.checksum_ = getUid(this.color_).toString();
} else {
this.checksum_ = 'f' + (this.color_ ?
_ol_color_.asString(this.color_) : '-');
this.checksum_ = 'f' + (this.color_ ? asString(this.color_) : '-');
}
}

View File

@@ -4,7 +4,7 @@
import {getUid, inherits} from '../index.js';
import ImageState from '../ImageState.js';
import {assert} from '../asserts.js';
import _ol_color_ from '../color.js';
import {asArray} from '../color.js';
import _ol_events_ from '../events.js';
import EventType from '../events/EventType.js';
import IconAnchorUnits from '../style/IconAnchorUnits.js';
@@ -101,8 +101,7 @@ var _ol_style_Icon_ = function(opt_options) {
* @private
* @type {ol.Color}
*/
this.color_ = options.color !== undefined ? _ol_color_.asArray(options.color) :
null;
this.color_ = options.color !== undefined ? asArray(options.color) : null;
/**
* @private

View File

@@ -1,7 +1,7 @@
/**
* @module ol/style/IconImageCache
*/
import _ol_color_ from '../color.js';
import {asString} from '../color.js';
/**
* Singleton class. Available through {@link ol.style.iconImageCache}.
@@ -36,7 +36,7 @@ var IconImageCache = function() {
* @return {string} Cache key.
*/
function getKey(src, crossOrigin, color) {
var colorString = color ? _ol_color_.asString(color) : 'null';
var colorString = color ? asString(color) : 'null';
return crossOrigin + ':' + src + ':' + colorString;
}