| Author |
Message |
ZeAL |
| Location: |
|
Post subject: Milih warna diantara 2 warna?
Posted: 13/Sep/2005 14:43
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
Gue lagi iseng ngotak-ngatik warna di canvas...
1.
Color itu pakenya kan TColor.. nah ada gak perintah untuk ngerubah ke RGB..?? ada sih perintah ColorToRgb tapi hasilnya cuma longint... Gue maunya R ditampung sendiri, G ditampung sendiri, B ditampung sendiri..
Oia, ada perintah untuk mbalikin lagi gak dari rgb ke TColor..???
2.
Trus ada yang tau cara ngambil warna diantara 2 warna..?? misalnya gue punya warna hitam dan warna putih.. nah warna diantaranya adalah abu-abu... Tapi gak cuma grayscale (hitam/putih) aja, tapi juga berwarna.. misalnya gue punya warna merah dan warna biru, maka warna diantaranya adalah ungu... Gimana donks.. ada yang bisa..?? algoritmanya juga gak pa2.. mentok nihh..
tengkyu berats sebelumnya... |
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
boandrenalin |
| Location: jkt |
|
Post subject:
Posted: 13/Sep/2005 16:24
|
|
onNovice

Joined: 23-Jun-2005
Posts: 14
Location: jkt
Status: Offline
|
|
Jawaban no 1 :
Ubah aja longintnya menjadi heksa 6 digit..ntu akan didapat nilai RGB nya tp masih dalam bentuk heksa..RGB dalam format heksa direpresentasikan begini
XX XX XX
R__G__B
so misal
00 00 FF warnanya akan merah
00 FF 00 warnanya akan hijau
FF FF FF warnanya akan hitam
so ubah aja longintnya ke heksa 6 digit trus pecah 2 digit 2 digit dulu...trus dari 2 digit heksa ntu ubah ke integer lagi..dapet deh
Untuk mengubah longint ke heksa 6 digit gunakan ini fungsi bawaan delphi
function IntToHex (Value: Integer; Digits: Integer): string;
trus ntuk ngubah heksa ke integer lagi gunakan ini
function HexToInt(HexNum: string): LongInt;
begin
Result:=StrToInt('$' + HexNum) ;
end;
Jawaban no 2 :
Maaf saya tidak bisa pak..semalam ga belajar  |
_________________ DELPHI = nDELete PHIkiran
|
| |
|
|
|
 |
kifmesoft |
| Location: |
|
Post subject: RE: Milih warna diantara 2 warna?
Posted: 13/Sep/2005 20:59
|
|
onKnowledgeable

Joined: 02-Apr-2005
Posts: 176
Status: Offline
|
|
|
ZeAL wrote:
2.
Trus ada yang tau cara ngambil warna diantara 2 warna..??
Bisa dg mengambil nilai rata2nya ... (jumlahkan masing2 RGB kemudian dibagi)
Contoh :
- Letakkan 3 komponen "Shape" dan 1 komponen "ColorDialog" ke Form
- Bikin Event "OnMouseDown" untuk masing2 komponen Shape
- Isikan masing2 Event dg kode berikut :
Code:
procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If ColorDialog1.Execute Then
Shape1.Brush.Color:= ColorDialog1.Color;
end;
procedure TForm1.Shape3MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If ColorDialog1.Execute Then
Shape3.Brush.Color:= ColorDialog1.Color;
end;
procedure TForm1.Shape2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Var
R, G, B: Integer;
begin
R:= (GetRValue(Shape1.Brush.Color) +
GetRValue(Shape3.Brush.Color)) Div 2;
G:= (GetGValue(Shape1.Brush.Color) +
GetGValue(Shape3.Brush.Color)) Div 2;
B:= (GetBValue(Shape1.Brush.Color) +
GetBValue(Shape3.Brush.Color)) Div 2;
Shape2.Brush.Color:= TColor(RGB(R, G, B));
end;
- Run ...
- klik komponen "Shape1" ... pilih warnanya
- klik komponen "Shape3" ... pilih warnanya
- klik komponen "Shape2" ... lihat hasil warna tengahnya ..
CMIIW  |
|
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 07:53
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
Wahh... tengkyu....
oia, ini ada code dari tempat yang lain...
Code:
type
TRGB = record
R: Integer;
G: Integer;
B: Integer;
end;
function RGBToColor(PR, PG, PB: Integer): TColor;
begin
Result := TColor((PB * 65536) + (PG * 256) + PR);
end;
function ColorToRGB(PColor: TColor): TRGB;
var
i: Integer;
begin
i := PColor;
Result.R := 0;
Result.G := 0;
Result.B := 0;
while i - 65536 >= 0 do
begin
i := i - 65536;
Result.B := Result.B + 1;
end;
while i - 256 >= 0 do
begin
i := i - 256;
Result.G := Result.G + 1;
end;
Result.R := i;
end;
{Convert a TRGB value to TColor}
function RGBToCol(PRGB: TRGB): TColor;
begin
Result := RGBToColor(PRGB.R, PRGB.G, PRGB.B);
end;
function GetPigmentBetween(P1, P2, Percent: Double): Integer;
begin
Result := Round(((P2 - P1) * Percent) + P1);
if Result > 255 then
Result := 255;
if Result < 0 then
Result := 0;
end;
|
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 08:11
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
lupa, mo nanya lagi...
Misalnya kita udah "main-main" di canvas.. nah, kalo ada window aplikasi lain muncul dan menimpa canvas, semua hasil main-main yang tertimpa akan hilang.. mau di repaint ulang, prosesnya terlalu lama.. ada cara biar gak begini..???
trus masih berhubungan.. misal ukuran canvas yang digunakan 200 x 100, tapi gambar yang mau ditampilkan/diedit/di-mainkan ukurannya lebih dari itu... Nah, masalahnya sama dengan yang diatas..
Untuk "mengakali" agar gambarnya bisa digeser-geser, gue pake komponen Scrollbox dan paintbox-nya di letakkan didalamnya... tapi ketika digeser, semua hasil "main-main" akan hilang... Nah, ada ide biar gak begini..??
tengkyuuuu... |
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
|
deLogic |
| Location: Jakarta |
|
Post subject:
Posted: 14/Sep/2005 08:52
|
|
onMage
Joined: 04-Jul-2005
Posts: 2282
Location: Jakarta
|
|
bicara masalah konversi warna, kenapa harus pake aritmatika 65536 dan 256, itu terlalu lama, lebih cepat pake perintah SHR dan SHL.
kalo x * 256, maka cukup x shl 8,
kalo x * 65536, maka cukup x shl 16.
jadi, RGB to Color nya menjadi:
Code:
function RGBToColor(PR, PG, PB: Integer): TColor;
begin
Result := TColor((PB shl 16) + (PG shl 8) + PR);
end;
nah terus konversi Color To RGB, kayaknya lebih gampang begini:
Code:
function ColorToRGB(PColor: TColor): TRGB;
begin
Result.R := PColor and $FF;
Result.G := (PColor shr 8) and $FF;
Result.B := (PColor shr 16) and $FF;
end;
btw snippet code tsb belum di test, jadi silahkan test sendiri ya... kekeke ..  |
|
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 13:33
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
tambahin pertanyaannya yah...
Gimana caranya kalo gradasi 4 warna dari sudut yang berbeda (kotak)... Ada yang bisa..????
(diketahui warna ke-4 sudut kotak, panjang & lebar sisi kotak).
Buat tambahan referensi.. ini tambahan dari kode yang gue kasih sebelumnya..
Gradasi 3 warna.. tapi sayangnya berupa garis, bukan kotak...(sama kayak sebelumnya)
Code:
function GetGradientColor3(R1, G1, B1, R2, G2, B2, R3, G3, B3, Percent: Double): Integer;
{Gets a color that is inbetween the color spread defined (R1,G1,B1), (R2,G2,B2) and (R3,G3,B3).
This is similar to GetGradientColor2, except that it allows you to specify 3 colors instead of 2.}
begin
{Use GetGradient2 to do most the work}
if Percent < 0.5 then
Result := GetGradientColor2(R1, G1, B1, R2, G2, B2, Percent * 2)
else
Result := GetGradientColor2(R2, G2, B2, R3, G3, B3, (Percent - 0.5) * 2);
end;
|
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
|
deLogic |
| Location: Jakarta |
|
Post subject:
Posted: 14/Sep/2005 14:07
|
|
onMage
Joined: 04-Jul-2005
Posts: 2282
Location: Jakarta
|
|
| ZeAL, itu fungsi GetGradientColor2 nya ada dimana..???? |
_________________ .:: Lagi gak pengen pasang signature ::.
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 14:18
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
oia, lupa...nguk...nguk..nguk...
ini yang lengkap deh...
Quote:
type
TRGB = record
R: Integer;
G: Integer;
B: Integer;
end;
function RGBToColor(PR, PG, PB: Integer): TColor;
begin
Result := TColor((PB * 65536) + (PG * 256) + PR);
end;
function ColorToRGB(PColor: TColor): TRGB;
var
i: Integer;
begin
i := PColor;
Result.R := 0;
Result.G := 0;
Result.B := 0;
while i - 65536 >= 0 do
begin
i := i - 65536;
Result.B := Result.B + 1;
end;
while i - 256 >= 0 do
begin
i := i - 256;
Result.G := Result.G + 1;
end;
Result.R := i;
end;
{Convert a TRGB value to TColor}
function RGBToCol(PRGB: TRGB): TColor;
begin
Result := RGBToColor(PRGB.R, PRGB.G, PRGB.B);
end;
function ColorFromRGB(Red, Green, Blue: Integer): Integer;
{Returns the color made up of the red, green, and blue components. Red, Green, and Blue can
be from 0 to 255.}
begin
{Convert Red, Green, and Blue values to color.}
Result := Red + Green * 256 + Blue * 65536;
end;
function GetPigmentBetween(P1, P2, Percent: Double): Integer;
begin
Result := Round(((P2 - P1) * Percent) + P1);
if Result > 255 then
Result := 255;
if Result < 0 then
Result := 0;
end;
function GetGradientColor2(R1, G1, B1, R2, G2, B2, Percent: Double): Integer;
{Gets a color that is inbetween the colors defined by (R1,G1,B1) and (R2,G2,B2)
Percent ranges from 0 to 1.0 (i.e. 0.5 = 50%)
If percent =0 then the color of (R1,G1,B1) is returned
If Percent =1 then the color of (R2,G2,B2) is returned
if Percent is somewhere inbetween, then an inbetween color is returned.}
var
NewRed, NewGreen, NewBlue: Integer;
begin
{Validate input data in case it is off by a few thousanths.}
if Percent > 1 then
Percent := 1;
if Percent < 0 then
Percent := 0;
{Calculate Red, green, and blue components for the new color.}
NewRed := GetPigmentBetween(R1, R2, Percent);
NewGreen := GetPigmentBetween(G1, G2, Percent);
NewBlue := GetPigmentBetween(B1, B2, Percent);
{Convert RGB to color}
Result := ColorFromRGB(NewRed, NewGreen, NewBlue);
end;
function GetGradientColor3(R1, G1, B1, R2, G2, B2, R3, G3, B3, Percent: Double): Integer;
{Gets a color that is inbetween the color spread defined (R1,G1,B1), (R2,G2,B2) and (R3,G3,B3).
This is similar to GetGradientColor2, except that it allows you to specify 3 colors instead of 2.}
begin
{Use GetGradient2 to do most the work}
if Percent < 0.5 then
Result := GetGradientColor2(R1, G1, B1, R2, G2, B2, Percent * 2)
else
Result := GetGradientColor2(R2, G2, B2, R3, G3, B3, (Percent - 0.5) * 2);
end;
|
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 14:50
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 14/Sep/2005 14:51
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
- EDIT : Sorry, Double Post -
@deLogic: Baru mau di apus lo dah post duluan jadi gak bisa di delete..
Jadi maksudnya gradien 4 sudut ituu...err...
Anggap sebuah kotak persegi panjang...
tiap sudut sebut aja A, B, C dan D... Tiap sudut masing-masing punya warna yang berbeda-beda.. Nah, trus lakukan proses gradien deh sampe ketengah-tengah kotak...
Ngerti kan..??? Gue gak ada contoh gambarnya soalnya.. |
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
Last edited by ZeAL on 14/Sep/2005; edited 1 time in total
|
| |
|
|
|
 |
|
deLogic |
| Location: Jakarta |
|
Post subject:
Posted: 14/Sep/2005 14:58
|
|
onMage
Joined: 04-Jul-2005
Posts: 2282
Location: Jakarta
|
|
@ZeAL:
yaela double post tuh....
masih belum begitu 100% paham tuh gradient 4 sudut bentuk kotak, bro, mungkin bisa ditambahin gambar bro, hasil yg diinginkan seperti apa.... biar temen2 gak salah kasih kode .... |
_________________ .:: Lagi gak pengen pasang signature ::.
|
| |
|
|
|
 |
kifmesoft |
| Location: |
|
Post subject: RE: Milih warna diantara 2 warna?
Posted: 16/Sep/2005 07:53
|
|
onKnowledgeable

Joined: 02-Apr-2005
Posts: 176
Status: Offline
|
|
|
ZeAL wrote:
Jadi maksudnya gradien 4 sudut ituu...err...
Anggap sebuah kotak persegi panjang...
tiap sudut sebut aja A, B, C dan D... Tiap sudut masing-masing punya warna yang berbeda-beda.. Nah, trus lakukan proses gradien deh sampe ketengah-tengah kotak...
modal dasarnya kan gradien 2 warna ...
(pegang itu dulu) selebihnya akan menjadi mudah
kira2 begini ... :
- lakukan gradien dari warna kiri atas dan kanan atas, simpan ke bufer-1
- lakukan gradien dari warna kiri bawah dan kanan bawah, simpan ke bufer-2
- lakukan gradien dari warna bufer-1 dan bufer-2 ke "Canvas"
kodenya kira2 begini :
Code:
Procedure Grad4Sudut(KiriAtas, KananAtas,
KiriBawah, KananBawah: TColor; Canvas: TCanvas);
Var
BufTop, BufBottom: TList;
c, x, y: Integer;
step: Integer;
Begin
BufTop:= TList.Create;
BufBottom:= TList.Create;
c:= (Canvas.ClipRect.Right + 1) - Canvas.ClipRect.Top;
BufTop.Count:= c;
BufBottom.Count:= c;
For x:= 0 To c - 1 Do
Begin
step:= MulDiv(255, x, c);
BufTop.Items[x]:= Pointer(RGB(
GetRValue(KiriAtas) +
MulDiv(step, GetRValue(KananAtas) - GetRValue(KiriAtas), 255),
GetGValue(KiriAtas) +
MulDiv(step, GetGValue(KananAtas) - GetGValue(KiriAtas), 255),
GetBValue(KiriAtas) +
MulDiv(step, GetBValue(KananAtas) - GetBValue(KiriAtas), 255)));
BufBottom.Items[x]:= Pointer(RGB(
GetRValue(KiriBawah) +
MulDiv(step, GetRValue(KananBawah) - GetRValue(KiriBawah), 255),
GetGValue(KiriBawah) +
MulDiv(step, GetGValue(KananBawah) - GetGValue(KiriBawah), 255),
GetBValue(KiriBawah) +
MulDiv(step, GetBValue(KananBawah) - GetBValue(KiriBawah), 255)));
End;
For x:= 0 To c - 1 Do
Begin
For y:= Canvas.ClipRect.Top To Canvas.ClipRect.Bottom Do
Begin
step:= MulDiv(255, y, (Canvas.ClipRect.Bottom + 1) -
Canvas.ClipRect.Top);
Canvas.Pixels[Canvas.ClipRect.Left + x, y]:= RGB(
GetRValue(TColor(BufTop.Items[x])) +
MulDiv(step, GetRValue(TColor(BufBottom.Items[x])) -
GetRValue(TColor(BufTop.Items[x])), 255),
GetGValue(TColor(BufTop.Items[x])) +
MulDiv(step, GetGValue(TColor(BufBottom.Items[x])) -
GetGValue(TColor(BufTop.Items[x])), 255),
GetBValue(TColor(BufTop.Items[x])) +
MulDiv(step, GetBValue(TColor(BufBottom.Items[x])) -
GetBValue(TColor(BufTop.Items[x])), 255));
End;
End;
BufTop.Free;
BufBottom.Free;
End;
- Letakkan 1 buah komponen PaintBox dan 1 komponen button ke Form ...
- Trus begini deh selanjutnya ,,
Code:
Procedure TForm1.Button1Click(Sender: TObject);
Begin
Grad4Sudut(clRed, clYellow, clLime, clBlue, PaintBox1.Canvas);
End;
|
|
|
| |
|
|
|
 |
ZeAL |
| Location: |
|
Post subject:
Posted: 16/Sep/2005 08:20
|
|
onProfessional

Joined: 07-Apr-2005
Posts: 763
Status: Offline
|
|
Kyaaaaa...... you're my hero..!
akhirnyaaaa....
Tengkyu berats nih...
oia, pertanyaan gue yang satu lagi ada yang bisa jawab gak..??
yang ini nih..
Code:
Misalnya kita udah "main-main" di canvas.. nah, kalo ada window aplikasi lain muncul dan menimpa canvas, semua hasil main-main yang tertimpa akan hilang.. mau di repaint ulang, prosesnya terlalu lama.. ada cara biar gak begini..???
trus masih berhubungan.. misal ukuran canvas yang digunakan 200 x 100, tapi gambar yang mau ditampilkan/diedit/di-mainkan ukurannya lebih dari itu... Nah, masalahnya sama dengan yang diatas..
Untuk "mengakali" agar gambarnya bisa digeser-geser, gue pake komponen Scrollbox dan paintbox-nya di letakkan didalamnya... tapi ketika digeser, semua hasil "main-main" akan hilang... Nah, ada ide biar gak begini..??
tengkyuuu... |
_________________ do i know you?
[ My Blog : http://www.monyetpinter.com ] [ My RPG Project : http://einarc.wordpress.com ] [ FJBex : http://www.fjbex.com ]
|
| |
|
|
|
 |
kifmesoft |
| Location: |
|
Post subject: RE: Milih warna diantara 2 warna?
Posted: 16/Sep/2005 10:42
|
|
onKnowledgeable

Joined: 02-Apr-2005
Posts: 176
Status: Offline
|
|
|
ZeAL wrote:
Untuk "mengakali" agar gambarnya bisa digeser-geser, gue pake komponen Scrollbox dan paintbox-nya di letakkan didalamnya... tapi ketika digeser, semua hasil "main-main" akan hilang... Nah, ada ide biar gak begini..??
ganti aja paintbox-nya dengan komponen TImage ...
sama2 punya "Canvas" kan ...  |
|
|
| |
|
|
|
 |
|
|
|