What are the ways to programmatically generate Material Design color sets?

前端 未结 2 1416
栀梦
栀梦 2021-02-07 22:04

I am trying to create a colour palette of Material Design that changing the lightness / luminosity by percentage with arbitrary color hex. When it comes to the implementation, I

2条回答
  •  花落未央
    2021-02-07 22:51

    Original problem

    Your code has error working with color format. Replace last line of hslToColor() like shown below and you'll make it run without errors:

    public static String hslToColor(int alpha, float hue, float saturation, float lightness) {
    
        ...
    
        // !!! ERROR WAS ON THE LAST LINE:
        return String.format("#%08x", resultColorInt).toUpperCase();
    }
    

    I've tested it - it works - because it makes 2 additional things:
    1) Formats value to have 8 digits
    2) Adds "#" prefix

    Possible SECOND problem in your code

    The alpha value may have values from 0 (transparent) to 255 (opaque). If you want to have opaque image you should pass 255 (0xFF).
    Now you pass 1 and I think it's an error - because it's almost transparent.
    So to have opaque color replace line

    String baseColor = hslToColor(1 ,baseColorHSL[0] , baseColorHSL[1] , (float)0.5);
    

    with

    String baseColor = hslToColor(0xFF ,baseColorHSL[0] , baseColorHSL[1] , (float)0.5);
    

    Annex
    If one needs to get a set of colors - a bit of creativity should be applied.
    To create a tint palette you have to change in a loop a) saturation or b) lightness or c) both of them.
    Here is an implementation example that returns palette based on lightness change from 0.4 to 0.6 (non inclusive) in 10 steps.
    "Experimental" means that you should find values for yourself.

    public static ArrayList returnMaterialDesignColorSet(String baseColorHex, int colorCount) {
        ArrayList resultList = new ArrayList();
         float [] baseColorHSL = colorToHsl(baseColorHex);
    
        float lght=0.4;// initial lightness value (experimental)
        float lStep=(0.6 - lght) / colorCount; // step to go up to 0.6 lightness (experimental)
        for (int i = 0; i < colorCount; i++) {
             String baseColor = hslToColor(1 ,baseColorHSL[0] , baseColorHSL[1] , lght);
             resultList.add(baseColor);
             lght += lStep;
        }
    
        return resultList;
    }
    

提交回复
热议问题