C#: Create a lighter/darker color based on a system color

前端 未结 11 1406
春和景丽
春和景丽 2020-12-07 18:26

Duplicate

How do I adjust the brightness of a color?
How do I determine darker or lighter color variant of a given color?
Programmat

11条回答
  •  一生所求
    2020-12-07 19:07

    I made a site that does this colorglower.com You can check it out to see a demo.

    Here's the javascript code i used.

    function lighten(color) {
    
    // convert to decimal and change luminosity
    var luminosity = 0.01
    var computedColors = new Array();
    var newColor = "#",
        c, i, n, black = 0,
        white = 255;
    for (n = 0; n < 10; n++) {
        for (i = 0; i < 3; i++) {
            c = parseInt(color.substr(i * 2, 2), 16);
            c = Math.round(Math.min(Math.max(black, c + (luminosity * white)), white)).toString(16);
            newColor += ("00" + c).substr(c.length);
        }
    
        computedColors[n] = newColor;
        var arrayUnique = checkIfArrayIsUnique(computedColors);
        if (arrayUnique == false) {
            computedColors.pop();
            break;
        }
    
        computedColors[n] = newColor;
        newColor = "#";
        luminosity += calcPercentage();
    }
    
    return computedColors;
    

    }

    What this code does is it receives a hex color and then it outputs 10 lightest color versions of it and puts in in the array. You can change the luminosity to whatever you like to adjust the shade percentage. To darken the colors you just need to change:

    luminosity -= calcPercentage();
    

提交回复
热议问题