All files index.js

96.55% Statements 28/29
94.12% Branches 16/17
100% Functions 6/6
96.55% Lines 28/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561x 1x 1x     19x 19x   1x       1x 20x 20x 20x 20x 1x     19x 19x 1x     18x     18x 1x   18x 1x     18x   15x 15x 15x       15x   3x 3x   3x              
let postcss = require('postcss');
let helpers = require('postcss-message-helpers');
let color = require('color');
 
function _handelParseColor(value) {
    try {
        return color(value);
    } catch (e) {
        return null;
    }
}
 
module.exports = postcss.plugin('postcss-make-it-bright', function (opts) {
    opts = opts || {};
    return function (style) {
        style.walkDecls(function (decl) {
            if (!decl.value) {
                return;
            }
 
            let inputColor = _handelParseColor(decl.value);
            if (!inputColor) {
                return;
            }
 
            let lighter = Number.isInteger(opts.lighter) ?
                Number(opts.lighter) / 100 :
                0.5;
            if (lighter > 1) {
                lighter = 1;
            }
            if (lighter < 0) {
                lighter = 0;
            }
 
            switch (inputColor.model) {
            case 'rgb':
                decl.value = helpers.try(() => {
                    const output = inputColor.lighten(lighter);
                    return decl.value.includes('rgb') ?
                        output.rgb() :
                        output.hex();
                }, decl.source);
                break;
            case 'hsl':
                decl.value = helpers.try(() => {
                    return inputColor.lighten(lighter);
                }, decl.source);
                break;
            default:
                return;
            }
        });
    };
});