"use strict";
/* Copyright 2015 Bloomberg Finance L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const d3 = __importStar(require("d3"));
// var d3 =Object.assign({}, require("d3-array"), require("d3-scale"));
const colorbrewer_1 = __importDefault(require("./colorbrewer"));
const default_scheme = 'RdYlGn';
//
// Returns the maximum number of colors available in the colorbrewer object
function get_max_index(color_object) {
    return d3.max(Object.keys(color_object).map(Number));
}
function cycle_colors(colors, count) {
    const colors_len = colors.length;
    if (colors_len > count) {
        return colors.slice(0, count);
    }
    else {
        let return_array = [];
        let iters = Math.floor(count / colors_len);
        for (; iters > 0; iters--) {
            return_array = return_array.concat(colors);
        }
        return return_array.concat(colors.slice(0, count % colors_len));
    }
}
exports.cycle_colors = cycle_colors;
function cycle_colors_from_scheme(scheme, num_steps) {
    scheme = (scheme in colorbrewer_1.default) ? scheme : default_scheme;
    const color_set = colorbrewer_1.default[scheme];
    // Indices of colorbrewer objects are strings
    let color_index = num_steps.toString();
    if (num_steps === 2) {
        return [color_set[3]["0"], color_set[3]["2"]];
    }
    else if (color_index in color_set) {
        return color_set[color_index];
    }
    else {
        color_index = get_max_index(color_set).toString();
        return this.cycle_colors(color_set[color_index], num_steps);
    }
}
exports.cycle_colors_from_scheme = cycle_colors_from_scheme;
function get_linear_scale(scheme) {
    scheme = ((scheme in colorbrewer_1.default) && !(colorbrewer_1.default[scheme]["type"] === "qual")) ?
        scheme : default_scheme;
    const color_set = colorbrewer_1.default[scheme];
    const color_index = get_max_index(color_set).toString();
    const colors = color_set[color_index];
    const scale = d3.scaleLinear().range(colors);
    return scale;
}
exports.get_linear_scale = get_linear_scale;
function get_ordinal_scale(scheme, num_steps) {
    const scale = d3.scaleOrdinal();
    scale.range(this.cycle_colors_from_scheme(scheme, num_steps));
    return scale;
}
exports.get_ordinal_scale = get_ordinal_scale;
function get_linear_scale_range(scheme) {
    return this.get_linear_scale(scheme).range();
}
exports.get_linear_scale_range = get_linear_scale_range;
function get_ordinal_scale_range(scheme, num_steps) {
    return this.get_ordinal_scale(scheme, num_steps).range();
}
exports.get_ordinal_scale_range = get_ordinal_scale_range;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ29sb3JVdGlscy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9Db2xvclV0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7Ozs7Ozs7OztHQWFHOzs7Ozs7Ozs7Ozs7QUFFSCx1Q0FBeUI7QUFDekIsdUVBQXVFO0FBQ3ZFLGdFQUF3QztBQUV4QyxNQUFNLGNBQWMsR0FBRyxRQUFRLENBQUE7QUFDL0IsRUFBRTtBQUNGLDJFQUEyRTtBQUMzRSxTQUFTLGFBQWEsQ0FBQyxZQUFZO0lBQy9CLE9BQU8sRUFBRSxDQUFDLEdBQUcsQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLFlBQVksQ0FBQyxDQUFDLEdBQUcsQ0FBQyxNQUFNLENBQUMsQ0FBQyxDQUFDO0FBQ3pELENBQUM7QUFFRCxTQUFnQixZQUFZLENBQUMsTUFBTSxFQUFFLEtBQUs7SUFDdEMsTUFBTSxVQUFVLEdB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