<commit_before>/**
 * Angular.js application controller
 *
 * @author eugene.trounev(a)gmail.com
 */

angular.module('app')
    .controller('HomeCtrl', [
        '$scope', 
        '$colorSerice', 
        '$routeParams', 
        '$location',
        'uiDialog',
        function($scope, $colorSerice, $routeParams, $location, uiDialog) {

            function _import(str){
                return angular.isDefined(str) && str.split('-').map(function(c){return '#' + c;});
            }

            function _export(pal){
                return {
                    string: pal.join(", "),
                    url: $location.absUrl().replace($location.path(), "/") + pal.join("-").replace(/#/g, ""),
                    json: angular.toJson(pal)
                }
            }

            function _generate(c, seq){
                var color = (c && $colorSerice(c)) || $colorSerice.random();
                switch(seq){
                    case 'analogous':
                        return color.analogous().map(function(t) { return t.toHexString(); });
                    break;
                    case 'monochromatic':
                        return color.monochromatic().map(function(t) { return t.toHexString(); });
                    break;
                    case 'tetrad':
                        return color.tetrad().map(function(t) { return t.toHexString(); });
                    break;
                    case 'triad':
                    default:
                        return color.triad().map(function(t) { return t.toHexString(); });
                }
                
            }

            $scope.palette = _import($routeParams["sequence"]) || _generate();

            $scope.mostReadable = function(c){
                return $colorSerice.mostReadable(c, ["#444", "#999", "#fff"]).toHexString();
            }

            $scope.add = function(hex) {
                if($scope.palette.length > 5) return;
                $scope.palette.push(hex || "#FFFFFF");
            }

            $scope.remove = function(index) {
                if($scope.palette.length < 2) return;
                $scope.palette.splice(index, 1);
            }

            $scope.generateAnalogous = function(index) {
                $scope.palette = _generate($scope.palette[index], 'analogous');
            }

            $scope.generateMonochromatic = function(index) {
                $scope.palette = _generate($scope.palette[index], 'monochromatic');
            }

            $scope.generateTriad = function(index) {
                $scope.palette = _generate($scope.palette[index], 'triad');
            }

            $scope.generateTetrad = function(index) {
                $scope.palette = _generate($scope.palette[index], 'tetrad');
            }

            $scope.share = function() {
                var data = _export($scope.palette);
                console.log(data.json);
                uiDialog.modal('templates/modal.share.html',
                    ['$scope', 'data', function($scope, data) {
                        $scope.data = data;
                    }],
                    {
                    resolve: {
                        data: function dataFactory() {
                            return data;
                        }
                    }
                });
            }

        }
    ]);<commit_msg>We can now move swatches and the color generation is fixed<commit_after>/**
 * Angular.js application controller
 *
 * @author eugene.trounev(a)gmail.com
 */

angular.module('app')
    .controller('HomeCtrl', [
        '$scope',
        '$colorSerice',
        '$exportSerice',
        '$routeParams',
        'uiDialog',
        function($scope, $colorSerice, $exportSerice, $routeParams, uiDialog) {

            function _generate(c, seq) {
                var color = (c && $colorSerice(c)) || $colorSerice.random();
                switch (seq) {
                    case 'analogous':
                        return color.analogous(1).map(function(t) {
                            return t.toHexString();
                        });
                        break;
                    case 'tetrad':
                        return color.tetrad().map(function(t) {
                            return t.toHexString();
                        });
                        break;
                    case 'triad':
                    default:
                        return color.triad().map(function(t) {
                            return t.toHexString();
                        });
                }

            }

            function _swap(array, index1, index2){
                array[index1] = array.splice(index2, 1, array[index1])[0];
            }

            $scope.palette = $exportSerice.import($routeParams["sequence"]) || _generate();

            $scope.mostReadable = function(c) {
                return $colorSerice.mostReadable(c, ["#444", "#999", "#fff"]).toHexString();
            }

            $scope.add = function() {
                if ($scope.palette.length > 5) return;
                $scope.palette.push($colorSerice($scope.palette.slice(-1)[0]).complement().toHexString());
            }

            $scope.moveLeft = function($index) {
                if ($index === 0) return;
                _swap($scope.palette, $index-1, $index);
            }

            $scope.moveRight = function($index) {
                if ($index === $scope.palette.length-1) return;
                _swap($scope.palette, $index+1, $index);
            }

            $scope.remove = function(index) {
                if ($scope.palette.length < 2) return;
                $scope.palette.splice(index, 1);
            }

            $scope.generateAnalogous = function(index) {
                $scope.palette = _generate($scope.palette[index], 'analogous');
            }

            $scope.generateTriad = function(index) {
                $scope.palette = _generate($scope.palette[index], 'triad');
            }

            $scope.generateTetrad = function(index) {
                $scope.palette = _generate($scope.palette[index], 'tetrad');
            }

            $scope.share = function() {
                var data = $exportSerice.export($scope.palette);
                console.log(data.json);
                uiDialog.modal('templates/modal.share.html', ['$scope', 'data',
                    function($scope, data) {
                        $scope.data = data;
                    }
                ], {
                    'resolve': {
                        data: function dataFactory() {
                            return data;
                        }
                    }
                });
            }

        }
    ]);
{'fraction_non_alphanumeric': 0.13560077231546117, 'fraction_numerical': 0.004752710530224268, 'mean_word_length': 1.2559463986599666, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3916493', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1688, 'n_words': 278}