// @flow
import type {BaseScale} from './baseScale';
import type Series from '../series/Series';

import * as d3Scale from 'd3-scale';
import categoricalScale from './categoricalScale';
import continuousScale from './continuousScale';
import baseScale from './baseScale';


type ColorScaleConfig = {
    series: Series,
    key: string,
    interpolate?: Function,
    range?: [number, number]
};

export type ColorScale = BaseScale & {
    type: 'color',
    scale: Function
};


export default function colorScale(config: ColorScaleConfig): ColorScale {
    const {interpolate, range, key, series} = config;
    let scale;

    if(interpolate) {
        const baseScale = continuousScale({key, series, range}).scale;
        scale = d3Scale.scaleSequential(interpolate).domain(baseScale.domain());
    } else {
        const baseScale = categoricalScale({key, series}).scale;
        scale = baseScale.range(range || baseScale.domain());
    }

    return baseScale({
        ...config,
        type: 'color',
        scale
    });

}

@@ -124,24 +124,82 @@
 'd3-scale';%0A
+import %7Bpiecewise, interpolateRgb%7D from 'd3-interpolate';%0A
 import categ
@@ -432,24 +432,46 @@
 e?: 
-%5Bnumber, number%5D
+Array%3Cstring%3E,%0A    set?: Array%3Cstring%3E
 %0A%7D;%0A
@@ -671,16 +671,21 @@
 , series
+, set
 %7D = conf
@@ -780,23 +780,16 @@
 , series
-, range
 %7D).scale
@@ -813,16 +813,29 @@
  d3Scale
+%0A            
 .scaleSe
@@ -843,28 +843,69 @@
 uential(
-interpolate)
+)%0A            .interpolator(interpolate)%0A            
 .domain(
@@ -934,13 +934,26 @@
    %7D
+%0A   
  else
+ if (set)
  %7B%0A 
@@ -1052,36 +1052,320 @@
 nge(
-range %7C%7C baseScale.domain())
+set);%0A    %7D%0A    else if (range) %7B%0A        const baseScale = continuousScale(%7Bkey, series%7D).scale;%0A        scale = d3Scale%0A            .scaleSequential()%0A            .interpolator(piecewise(interpolateRgb.gamma(2.2), range))%0A            .domain(baseScale.domain());%0A    %7D%0A    else %7B%0A        scale = point =%3E point
 ;%0A  

Add set and range to color scale, fix interpolate