import VisualVariable from './VisualVariable'
import { Zondy } from '../../../base'
import { defaultValue } from '../../../util'
import VisualVariableType from '../../../base/enum/VisualVariableType'
import ColorStop from './support/ColorStop'
/**
* color视觉变量
* @class ColorVariable
* @moduleEX RendererModule
* @param {Object} options 初始化参数
* @param {Array<ColorStop>} [options.stops] 颜色分段数组
*
* @example color视觉变量使用方式
* // 获取layer上renderer对象
* const renderer = layer.renderer
* // 根据FID取值设置#FFFCD4到#350242的过渡颜色值
* // FID取值小于0时,显示颜色#FFFCD4
* // FID取值大于200时,显示颜色#350242
* // FID取值在0-200之间时,显示颜色过渡值
* renderer.visualVariables = [{
type: "color",
valueExpression: "$feature.FID",
stops: [
{
color: "#FFFCD4",
value: 0,
},
{
color: "#350242",
value: 200,
},
],
}]
*
*/
class ColorVariable extends VisualVariable {
constructor(options) {
super(options)
options = defaultValue(options, {})
this.type = VisualVariableType.color
/**
* 归一化字段
* @member {Array<ColorStop>} ColorVariable.prototype.stops
*/
this.stops = defaultValue(options.stops, [])
this.stops = this.stops.map((v) => ColorStop.fromJSON(v))
}
/**
* 通过json构造ColorVariable对象
* @return {ColorVariable} json对象
* */
static fromJSON(json) {
return new ColorVariable(json)
}
/**
* 导出为json对象
* @return {Object} json对象
* */
toJSON() {
const json = super.toJSON()
json.type = this.type
json.normalizationField = this.normalizationField
json.stops = this.stops.map((v) => v.toJSON())
return json
}
/**
* 克隆对象
* @return {ColorVariable} 克隆后的ColorVariable对象
*/
clone() {
return new ColorVariable(this.toJSON())
}
}
Zondy.Renderer.VisualVariable.ColorVariable = ColorVariable
export default ColorVariable