2017-03-14 11 views
0

RGB/HEX 색상을 특정 색상 맵을 가진 해당 (정규화 된) 숫자 값으로 변환하고 싶습니다. 앞으로 작업을 수행 할 수있는 많은 유틸리티가 있습니다 (즉, 색상 표를 사용하여 정규화 된 값 집합을 RGB/HEX 색상으로 매핑 함). 그러나 반대로 수행 할 수는 없습니다.RGB/HEX to Colormap 값

앞으로 :

> 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.  ]]) 

어떻게 이러한 viridis에서 온 것을 알고, 물론, 0.2[ 0.253935, 0.265254, 0.529983, 1. ]에서받을 수 있나요?

답변

0

나는 역 매핑의 원리를 설명하는 Matlab 구현을 사용하여 예제를 제공 할 수있다.

파이썬 구현을 찾고있는 경우 질문에 파이썬 태그를 추가하십시오.

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) 

결과 : 당신은 아마 바로 가기와 같은 찾을 수 있습니다 파이썬에서

reverse_val = 

    0.2000 

: 문자열로 변환을 구축 여기

내 코드 샘플 (설명 댓글 이내)입니다 사전 (문자열에서 색인 또는 값으로) ...