2017-03-14 4 views
0

Ich möchte RGB/HEX-Farben zu einem entsprechenden (normalisierten) numerischen Wert bei einer bestimmten Colormap konvertieren. Es gibt viele Dienstprogramme, die die Vorwärtsoperation ausführen können (d. H. Eine Reihe von normalisierten Werten in RGB/HEX-Farben unter Verwendung einer Colormap abbilden), aber ich konnte keine umgekehrte Operation finden.RGB/HEX zu Colormap Wert

Forward:

> import matplotlib.cm as cm 
> cm.viridis([.2, .4, .6, .8, 1]) 

array([[ 0.253935, 0.265254, 0.529983, 1.  ], 
     [ 0.163625, 0.471133, 0.558148, 1.  ], 
     [ 0.134692, 0.658636, 0.517649, 1.  ], 
     [ 0.477504, 0.821444, 0.318195, 1.  ], 
     [ 0.993248, 0.906157, 0.143936, 1.  ]]) 

Wie kann ich [ 0.253935, 0.265254, 0.529983, 1. ]-0.2 bekommen, natürlich, wohl wissend, dass diese von viridis kommen?

Antwort

0

Ich kann Ihnen ein Beispiel mit Matlab-Implementierung geben (erklären Sie das Prinzip der umgekehrten Zuordnung).

Wenn Sie nach Python-Implementierung suchen, fügen Sie Python-Tag zu Ihrer Frage hinzu.

Hier ist mein Beispielcode (Erklärungen sind innerhalb von Kommentaren):

array = [0.253935, 0.265254, 0.529983, 1; 
     0.163625, 0.471133, 0.558148, 1; 
     0.134692, 0.658636, 0.517649, 1; 
     0.477504, 0.821444, 0.318195, 1; 
     0.993248, 0.906157, 0.143936, 1]; 

%c is the RGB value (value is assumed to exist in array). 
c = [0.253935, 0.265254, 0.529983, 1]; 

%B is the mapped value. 
B = [.2, .4, .6, .8, 1]; 

%1 Remove the alpha channel (assume all alpha values equal 1): 
A = array(:, 1:3); 

%2. Convert from normalized value in range [0, 1] to fixed point values in range [0, 255]. 
%(assume each color channel is a byte). 
A = round(A*255); 

%3. Convert RGB triple to single fixed point value (24 bits integer). 
% Remark: For best performance, you can create a Look Up Table (16MBytes look up table that maps all possible combinations). 
% Remark: You can also create a Dictionary. 
A = A(:,1) + A(:,2)*2^8 + A(:,3)*2^16; 

%4. Do the same conversion for c: 
c = round(c*255); 
c = c(1)+c(2)*2^8+c(3)*2^16; 

%5. Find index of c in A: 
% Remark: In case A is sorted, you can use binary search. 
% Remark: In case A is Look Up Table (or dictionary), you can use something like idx = A(c). 
idx = find(A == c); 

%6. The result is B in place idx: 
reverse_val = B(idx) 

Ergebnis:

reverse_val = 

    0.2000 

In Python Sie wahrscheinlich eine Abkürzung wie finden: Umwandlung in einen String, bauen ein Wörterbuch (von String zu Index oder zu Wert) ...