INNER CODE UNIT · JavaScript

nearest_color_index_in_palette

increpare/flickgame · flickgame_base.js:64

function nearest_color_index_in_palette(palette, target_color) {
    var nearest_idx = 0;
    var nearest_dist = color_distance(target_color, palette[0]);
    for (var i = 1; i < palette.length; i++) {
        var dist = color_distance(target_color, palette[i]);
        if (dist < nearest_dist) { nearest_dist = dist; nearest_idx = i; }
    }
    return nearest_idx;
}

function nearest_color_in_palette(palette, target_color) {
    return palette[nearest_color_index_in_palette(palette, target_color)];
}

// Returns "#ffffff" or "#000000" for best contrast against the given hex color
function contrastTextColor(hex) {
    var dw = color_distance(hex, "#ffffff");
    var db = color_distance(hex, "#000000");

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…