"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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.get_ordinal_scale_range = exports.get_linear_scale_range = exports.get_ordinal_scale = exports.get_linear_scale = exports.cycle_colors_from_scheme = exports.cycle_colors = void 0;
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=ColorUtils.js.map