通常、R = 1.0の色相を変えることを考えると
R = 1.0, G = 0.0, B = 0.0
R = 1.0, G = 1.0, B = 0.0
R = 0.0, G = 1.0, B = 0.0
R = 0.0, G = 1.0, B = 1.0
R = 0.0, G = 0.0, B = 1.0
R = 1.0, G = 0.0, B = 0.0
RとGとBの合計値が2.0になる場所が出てくるので、
R = 1.0, G = 0.0, B = 0.0
R = 0.5, G = 0.5, B = 0.0
R = 0.0, G = 1.0, B = 0.0
R = 0.0, G = 0.5, B = 0.5
R = 0.0, G = 0.0, B = 1.0
R = 0.5, G = 0.0, B = 0.5
のように、次の3つの場合を分けて考えます。
・Rを0で、GとBを変更。
・Gを0で、RとBを変更。
・Bを0で、RとGを変更。
例えば
色を変えない場合A
R' = 1.0 * R + 0.0 * G + 0.0 * B
G' = 0.0 * R + 1.0 * G + 0.0 * B
B' = 0.0 * R + 0.0 * G + 1.0 * B
ずらす場合B
R' = 0.0 * R + 1.0 * G + 0.0 * B
G' = 0.0 * R + 0.0 * G + 1.0 * B
B' = 1.0 * R + 0.0 * G + 0.0 * B
があって、AとBの間は
R' = 0.5 * R + 0.5 * G + 0.0 * B
G' = 0.0 * R + 0.5 * G + 0.5 * B
B' = 0.5 * R + 0.0 * G + 0.5 * B
となるように行列を作っていくのがポイントです。
#module "_huematrix_"
//0から1の間の値に変換
//-0.2は0.8。1.2は0.2とする。
#defcfunc normalize double x,local x2
x2 = x
x2 -= int(x)
if(x2 < 0f){x2 += 1.0}
return(x2)
//6通りの色相変換
//xは色相。0〜1で一周する。
#deffunc gethue1 double x,array arr,local x2,local hi,local f1,local f2,local m0,local m1,local m2
x2 = normalize(x)
hi = int(x2 * 6.0)
f2 = x2 * 6.0 - hi
f1 = 1.0 - f2
if(hi == 0) {m0 = 1f:m1 = f2:m2 = 0f}
else:if(hi == 1) {m0 = f1:m1 = 1f:m2 = 0f}
else:if(hi == 2) {m0 = 0f:m1 = 1f:m2 = f2}
else:if(hi == 3) {m0 = 0f:m1 = f1:m2 = 1f}
else:if(hi == 4) {m0 = f2:m1 = 0f:m2 = 1f}
else:if(hi == 5) {m0 = 1f:m1 = 0f:m2 = f1}
ddim arr, 20
arr = m0,m1,m2,0f,0f,m2,m0,m1,0f,0f,m1,m2,m0,0f,0f,0f,0f,0f,1f
return
//6通りの色相変換(hsvcolor)
//色相の分解能が192しかない
#deffunc gethue2 double x,array arr,local bmscr,local col,local m0,local m1,local m2
mref bmscr, 67
col = bmscr(40) ;色を保存
hsvcolor 192f * x, 255, 255
m0 = double(ginfo_r) / 255f
m1 = double(ginfo_g) / 255f
m2 = double(ginfo_b) / 255f
ddim arr, 20
arr = m0,m1,m2,0f,0f,m2,m0,m1,0f,0f,m1,m2,m0,0f,0f,0f,0f,0f,1f
color col>>16,(col>>8)&0xff,col&0xff;色をもとに戻す
return
//3通りの色相変換
#deffunc gethue3 double x,array arr,local x2,local hi,local f1,local f2,local m0,local m1,local m2
x2 = normalize(x)
hi = int(x2 * 3.0)
f2 = x2 * 3.0 - hi
f1 = 1.0 - f2
if(hi == 0) {m0 = f1:m1 = f2:m2 = 0f}
else:if(hi == 1) {m0 = 0f:m1 = f1:m2 = f2}
else:if(hi == 2) {m0 = f2:m1 = 0f:m2 = f1}
ddim arr, 20
arr = m0,m1,m2,0f,0f,m2,m0,m1,0f,0f,m1,m2,m0,0f,0f,0f,0f,0f,1f
return
#global
#include "./a2d.hsp"
#include "./llmod3/llmod3.hsp"
#include "./llmod3/trackbox.hsp"
screen 0,500,400,2
dialog "bmp;*.gif;*.jpg", 16
file=""+refstr
picload ""+file,1
pos 10,110:color:mes "元の画像"
gsel 0,1
pos 10,140
trackbox 400,30,$100
tb_id.0=stat
trackrng 0,1000
pos 10,260:objmode 0:button "変換",*hyouji
stop
*hyouji
alCreateImage 0,500,400
alCreateImageByFile 0, ""+file
sel_trackbox tb_id.0
trackpos 0,1
x = 0.001 * stat
gethue3 x, cmatrix
alCopyModeColorMatrix cmatrix
alCopyImageToScreen 0, 0,120,10,100,100
color 240,240,240:boxf 10,300,150,410
pos 120,110:color 0:mes "変換後の画像"
pos 10,300:mes "x= "+x
gsel 0,1
redraw 1
stop