スペクトラム変換

0.0 ~ 1.0 の数値をスペクトラム色の文字列(#0000FF ~ #FF0000)に変換

from math import *

def ColorScaleBCGYR(v):
    tmp_v = cos( 4. * pi * v )
    col_v =  int(( -tmp_v / 2. + 0.5 ) * 255)
    r = g = b = 0
    if v >= ( 4.0 / 4.0 ):
        r = 255
        g = b = 0
    elif v >= ( 3.0 / 4.0 ):
        r = 255
        g = col_v
        b = 0
    elif v >= ( 2.0 / 4.0 ):
        r = col_v
        g = 255
        b = 0
    elif v >= ( 1.0 / 4.0 ):
        r = 0
        g = 255
        b = col_v
    elif v >= ( 0.0 / 4.0 ):
        r = 0
        g = col_v
        b = 255
    else:
        r  = g = 0
        b = 255

    str_r = str(hex(r)).replace('0x','')
    str_g = str(hex(g)).replace('0x','')
    str_b = str(hex(b)).replace('0x','')
    if len(str_r) == 1:
        str_r = '0'+str_r
    if len(str_g) == 1:
        str_g = '0'+str_g
    if len(str_b) == 1:
        str_b = '0'+str_b
    return '#' + str_r + str_g + str_b

Comments are closed.