2009年3月18日 星期三

利用Canvas 中圖形色差造多邊形,包成function,感覺不是很快,400*400的圖要0.2秒左右,已經到能感覺出來的程度

function TForm1.CreateRgnByCanvasColorDiff(Can: TCanvas; Diff: Byte): HRGN;
var P:array of TPoint;
i,j,PCount:integer;
RGN:HRGN;
//計算是否有色差
function ColorDiff(ColorA, ColorB: TColor;Diff:Byte): boolean;
var c:TColor;
begin
c := (ColorA xor ColorB);
Result := not ((GetRValue(c) < Diff) and (GetGValue(c) < Diff) and (GetBValue(c) < Diff) );
end;
begin
//逐行掃描 , 如果 [I,J] <> [I,J+1] -->邊界, 算出左邊界 然後 右邊界
SetLength(P,Can.ClipRect.Bottom*2);
PCount := 0;
//取左邊界線
for j := 1 to Can.ClipRect.Bottom-1 do
for i := 1 to Can.ClipRect.Right-1 do
begin
if ColorDiff(Can.Pixels[i,j],Can.Pixels[i,j+1],Diff) then
begin
P[PCount].X := i;
P[PCount].Y := j;
Inc(PCount);
break;
end;
end;

//取右邊界線
for j := Can.ClipRect.Bottom-1 downto 1 do
for i := Can.ClipRect.Right-1 downto 1 do
begin
if ColorDiff(Can.Pixels[i,j],Can.Pixels[i,j-1],Diff) then
begin
//if i < 5 then break;
P[PCount].X := i;
P[PCount].Y := j;
Inc(PCount);
break;
end;
end;
Result := CreatePolygonRgn(P[0],PCount,ALTERNATE);

end;

簡單的顏色比對 , 計算顏色差 RGB各小於Byte的差異

function TForm1.ColorDiff(ColorA, ColorB: TColor;Diff:Byte): boolean;
var c:TColor;
begin
c := (ColorA xor ColorB);
Result := not ((GetRValue(c) < Diff) and (GetGValue(c) < Diff) and (GetBValue(c) < Diff) );
end;

還要做顏色比對

建立多邊形要利用顏色比對,但現在還沒想出怎樣顏色比對
現在用這種做法
Label1.Canvas.Pixels[i,j]<>Label1.Canvas.Pixels[i,j-1]
有缺點,只能比對黑白色圖,要想辦法做 顏色色差比對,要差一定比例才算邊界

用從上到下,從左到右掃描的方式也有漏洞, 當圖形是有內凹的凹的部分會算不到, 比如說五角星還OK,十二角星就不行了,兩星角之間的位置抓不到

當然三角形的按鈕就很容易實作

var
P: array[0..3] of TPoint;
FRegion: HRGN;
begin
P[0] := Point(0,100);
P[1] := Point(100,0);
P[2] := Point(200,100);

FRegion := CreatePolygonRgn(P, 3,ALTERNATE); //<-- 如果是動態陣列,第一個參數要傳P[0]
SetWindowRgn(Button1.Handle, FRegion, True);

多邊形製作,當然Panel,Form...都可以

參考以前一些文章,總是沒人寫很容易懂的範例,自己實作,對LABEL上的圖做比對,算出左右邊界線,變更Panel 的圖形


想法:從上到下從左到右掃描,當點素色Color不同時就當作邊界,要做一個封閉的多邊形,要先從左邊界連下來
再從右邊界往上連回去

procedure TForm1.Button1Click(Sender: TObject);
var PALL:array of TPoint;
i,j,PLCount:integer;
RGN:HRGN;
begin
//逐行掃描 , 如果 [I,J] <> [I,J+1] -->邊界, 算出左邊界 然後 右邊界

SetLength(PALL,Label1.ClientHeight*2);
PLCount := 0;

//取左邊界線
for i := 1 to Label1.ClientHeight-1 do
for j := 1 to Label1.ClientWidth-1 do
begin
if Label1.Canvas.Pixels[i,j]<>Label1.Canvas.Pixels[i,j+1] then
begin
PALL[PLCount].X := i;
PALL[PLCount].Y := j;
Inc(PLCount);
break;
end;
end;

//取右邊界線
for i := Label1.ClientHeight-1 downto 1 do
for j := Label1.ClientWidth-1 downto 1 do
begin
if Label1.Canvas.Pixels[i,j]<>Label1.Canvas.Pixels[i,j-1] then
begin
PALL[PLCount].X := i;
PALL[PLCount].Y := j;
Inc(PLCount);
break;
end;
end;

RGN := CreatePolygonRgn(PALL[0],PLCount,ALTERNATE);//建立一個多邊形區域

SetWindowRgn(Panel1.Handle,RGN,True);// 可以變Panel,當然其他物件也可以


end;

alpha blending 的做法大概就是如此,看wiki的

基本原理是這樣,但這樣算我想應該會很慢....而且也沒這麼好實做到delphi物件上,
因為alphi blending 需要合併上下物件顏色來使上層物件顏色透明,但抓不到下層物件顏色就白說了
delphi把物件PAINT 封裝的太完整,想要切到裡面去改...很麻煩



var t:TColor;
R,G,B:Byte;
begin
R := Round((1-0.5) * GetRValue( Form1.Color) + 0.5 * GetRValue( Label1.Color));
G := Round((1-0.5) * GetGValue( Form1.Color) + 0.5 * GetGValue( Label1.Color));
B := Round((1-0.5) * GetBValue( Form1.Color) + 0.5 * GetBValue( Label1.Color));
Label1.Color := RGB(R,G,B);

不規則按鈕

procedure TForm1.Button3Click(Sender: TObject);
var
Region1 : THandle;
begin
Region1 :=Button1.Handle;
Region1 := CreateEllipticRgn(10,10,Button1.Width-4,Button1.Height-4);
SetWindowRgn(Button1.Handle, Region1, True );//<--重點在可以設定Rgn 我猜不規則多邊形應該也可以
end;