Create a hexadecimal colour based on a string with JavaScript

后端 未结 13 925
旧时难觅i
旧时难觅i 2020-11-30 17:15

I want to create a function that will accept any old string (will usually be a single word) and from that somehow generate a hexadecimal value between #000000

13条回答
  •  旧巷少年郎
    2020-11-30 18:09

    I convert this for Java.

    Tanks for all.

    public static int getColorFromText(String text)
        {
            if(text == null || text.length() < 1)
                return Color.BLACK;
    
            int hash = 0;
    
            for (int i = 0; i < text.length(); i++)
            {
                hash = text.charAt(i) + ((hash << 5) - hash);
            }
    
            int c = (hash & 0x00FFFFFF);
            c = c - 16777216;
    
            return c;
        }
    

提交回复
热议问题