Mapping arbitrary strings to RGB values

前端 未结 8 1295
迷失自我
迷失自我 2021-01-05 03:49

I have a huge set of arbitrary natural language strings. For my tool to analyze them I need to convert each string to unique color value (RGB or other). I need color contras

相关标签:
8条回答
  • 2021-01-05 04:22

    How important is it that you never end up with two dissimilar strings having the same colour?

    If it's not that important then maybe this could work?

    You could pick a 1 dimensional color space that is "homotopic" to the circle: Say the color function c(x) is defined for x between 0 and 1. Then you'd want c(0) == c(1).

    Now you take the sum of all character values modulo some scaling factor and wrap this back to the color space:

    c( (SumOfCharValues(word) modulo ScalingFactor) / ScalingFactor )

    This might work even better if you defined a "wrapping" color space of higher dimensions and for each dimension pick different SumOfCharValues function; someone suggested alternating sum and length.

    Just a thought... HTH

    0 讨论(0)
  • 2021-01-05 04:23

    I would maybe define some delta between two strings. I don't know what you define as the difference (or "unequality") of two strings, but the most obvious thing I could think about would be string length and the number of occurences of particular letters (and their index in the string). It should not be tricky to implement it such that it returns the same color code in equal strings (if you do an equal first, and return before further comparison).

    When it comes to the actual RGB value, I would try to convert the string data into 4 bytes (RGBA), or 3 bytes if you only use the RGB. I don't know if every string would fit into them (as that may be language specific?).

    0 讨论(0)
提交回复
热议问题