<commit_before>// MapPreview
// ----------
// A web map client for displaying tile-based maps.
view = Backbone.View.extend({
    className: 'MapClient',
    id: 'map',
    initialize: function(options) {
        _.bindAll(this, 'ready', 'record', 'mm', 'mmNav');
    },
    ready: function() {
        var wax = this.generateWax();
        if (!wax.layers || wax.layers.length === 0) return;

        $.ajax({
            dataType: 'jsonp',
            url: this.waxURL(wax),
            context: this,
            callback: 'grid',
            callbackParameter: 'callback',
            success: this.record,
            error: function() {}
        });
    },
    waxURL: function(wax) {
        return this.model.options.uiHost + 'api/wax.json?' + $.param(wax);
    },
    generateWax: function(callback) {
        var wax = this.model.wax();
        wax.el = $(this.el).attr('id');
        wax.size && (delete wax.size);
        return wax;
    },
    record: function(data) {
        if (data && data.wax) {
            var api = this.generateWax().api;
            this.map = wax.Record(data.wax);
            _(this[api]).isFunction() && this[api]();
        }

        // Calculate the background luminosity from the first tile.
        setTimeout(function() {
            var tile = this.$('img:first')[0];
            if (!tile) return;
            if (tile.complete) setBackground();
            else $(tile).load(setBackground);

            function setBackground() {
                if (view.alphaImageLuminosity(tile) > 0.9) {
                    $(tile).closest('.MapClient').css({ backgroundColor: 'black' });
                }
            }
        }.bind(this), 0);
    },
    mm: function() {
        this.map.addCallback('zoomed', this.mmNav);
        this.mmNav();
    },
    mmNav: function() {
        if (!$('.zoom').size()) return;
        $('.zoom.active').removeClass('active');
        $('.zoom-' + this.map.getZoom()).addClass('active');
    }
});

// Calculates the luminosity (0-1) of all semi-transparent pixels.
view.alphaImageLuminosity = function(el) {
    if (!HTMLCanvasElement || !el.complete) return -1;

    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', el.width);
    canvas.setAttribute('height', el.height);
    var c = canvas.getContext('2d');
    c.drawImage(el, 0, 0);

    var r = 0, g = 0, b = 0, total = 0;
    var pixels = c.getImageData(0, 0, el.width, el.height).data;

    // Look at every pixel's alpha value and only proceed if it is semi-transparent.<commit_msg>adjust threshold<commit_after>// MapPreview
// ----------
// A web map client for displaying tile-based maps.
view = Backbone.View.extend({
    className: 'MapClient',
    id: 'map',
    initialize: function(options) {
        _.bindAll(this, 'ready', 'record', 'mm', 'mmNav');
    },
    ready: function() {
        var wax = this.generateWax();
        if (!wax.layers || wax.layers.length === 0) return;

        $.ajax({
            dataType: 'jsonp',
            url: this.waxURL(wax),
            context: this,
            callback: 'grid',
            callbackParameter: 'callback',
            success: this.record,
            error: function() {}
        });
    },
    waxURL: function(wax) {
        return this.model.options.uiHost + 'api/wax.json?' + $.param(wax);
    },
    generateWax: function(callback) {
        var wax = this.model.wax();
        wax.el = $(this.el).attr('id');
        wax.size && (delete wax.size);
        return wax;
    },
    record: function(data) {
        if (data && data.wax) {
            var api = this.generateWax().api;
            this.map = wax.Record(data.wax);
            _(this[api]).isFunction() && this[api]();
        }

        // Calculate the background luminosity from the first tile.
        setTimeout(function() {
            var tile = this.$('img:first')[0];
            if (!tile) return;
            if (tile.complete) setBackground();
            else $(tile).load(setBackground);

            function setBackground() {
                if (view.alphaImageLuminosity(tile) > 0.8) {
                    $(tile).closest('.MapClient').css({ backgroundColor: 'black' });
                }
            }
        }.bind(this), 0);
    },
    mm: function() {
        this.map.addCallback('zoomed', this.mmNav);
        this.mmNav();
    },
    mmNav: function() {
        if (!$('.zoom').size()) return;
        $('.zoom.active').removeClass('active');
        $('.zoom-' + this.map.getZoom()).addClass('active');
    }
});

// Calculates the luminosity (0-1) of all semi-transparent pixels.
view.alphaImageLuminosity = function(el) {
    if (!HTMLCanvasElement || !el.complete) return -1;

    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', el.width);
    canvas.setAttribute('height', el.height);
    var c = canvas.getContext('2d');
    c.drawImage(el, 0, 0);

    var r = 0, g = 0, b = 0, total = 0;
    var pixels = c.getImageData(0, 0, el.width, el.height).data;

    // Look at every pixel's alpha value and only proceed if it is semi-transparent.
{'fraction_non_alphanumeric': 0.16872186337835193, 'fraction_numerical': 0.006654922685457036, 'mean_word_length': 2.034441805225653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6501095', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1522, 'n_words': 354}