Author 
Topic: color picker (Read 517 times) 

zzz000abc
Full Member
member is offline
Gender:
Posts: 256


color picker
« Thread started on: May 16^{th}, 2017, 05:11am » 

hi, here is the code for basic model of color picker: Code: nomainwin
pi=3.14159265
ww=DisplayWidth/2
wh=DisplayHeight/2
WindowWidth=ww
WindowHeight=wh
graphicbox #1.gb 0,10,ww/2,ww/2
textbox #1.tb1 ww/2+10,40,100,30
textbox #1.tb2 ww/2+10+100+10,40,100,30
textbox #1.tb3 ww/2+10+200+20,40,100,30
textbox #1.tb4 ww/2+10,150,300,40
textbox #1.tb5 ww/2+10,150+70,300,40
textbox #1.tb6 ww/2+10,150+140,300,40
open"JB color pick"for window as#1
#1 "trapclose [quit]"
dc=(atn(1))/45
xx=ww/4:yy=wh/3'centre
leg=ww/6:stk=20 'leg and stroke lengths
clr1$="blue":clr2$="white"
leg=100
call cring xx,yy,leg,0,0
leg=leg20
call try60 xx,yy,i,leg,stk,clr1$,sclr$
sktclr$(1)=" 255 0 0"
#1.tb1 "!font tahoma 12":#1.tb1 " red"
#1.tb2 "!font tahoma 12":#1.tb2 " green"
#1.tb3 "!font tahoma 12":#1.tb3 " blue"
#1.tb4 "!font tahoma 15"
#1.tb5 "!font tahoma 15"
#1.tb6 "!font tahoma 15"
#1.gb "when leftButtonUp [mup] "
wait
[mup]
mx=MouseX:my=MouseY
Dy=yymy:Dx=mxxx
ang=atan2(Dy,Dx)
r1=Dx/cos(ang)
if ang<0 then ang=2*pi+ang
if r1>=leg and r1<=leg+stk then
sclr$=sktclr$(1)
call cring xx,yy,leg,1,(360ang/dc)
call try60 xx,yy,i,leg,stk,clr2$,sclr$
sclr$="black"
call try60 xx,yy,ang/dc,leg,stk,clr1$,sclr$
i=ang/dc
tbclr$=sktclr$(1)
r=int(val(word$(tbclr$,1)))
g=int(val(word$(tbclr$,2)))
b=int(val(word$(tbclr$,3)))
#1.gb "color ";abs(240r);" ";abs(240g);" ";abs(240b)
#1.gb "backcolor ";sktclr$(1)
#1.gb "up;goto 300 300;down;circlefilled 20;flush"
#1.gb "backcolor white"
#1.tb1 " ";r
#1.tb2 " ";g
#1.tb3 " ";b
#1.tb4 "color ";tbclr$
#1.tb5 "backcolor ";tbclr$
#1.tb6 "fill ";tbclr$
end if
wait
[quit]
close #1
end
sub try60 x0,y0,ang0,leg,stk,clr$,sclr$
dc=(atn(1))/45 :pi=4*atn(1)
rd1=(ang0)*dc
rd2=(ang0+60)*dc
rd3=(ang0+120)*dc
x1=x0+leg*cos(rd1)
y1=y0leg*sin(rd1)
x2=x0leg*cos(rd2)
y2=y0+leg*sin(rd2)
x3=x0+leg*cos(rd3)
y3=y0leg*sin(rd3)
x11=x0+(leg+stk)*cos(rd1)
y11=y0(leg+stk)*sin(rd1)
#1.gb "discard;color ";clr$
#1.gb "down;size 2;line ";x1;" ";y1;" ";x2;" ";y2
#1.gb "line ";x1;" ";y1;" ";x3;" ";y3
#1.gb "line ";x2;" ";y2;" ";x3;" ";y3
if sclr$<>"" then #1.gb "size 3;color ";sclr$
#1.gb "line ";x11;" ";y11;" ";x1;" ";y1;"; flush"
#1.gb "color ";clr$
end sub
sub cring x0,y0,rd,s,ang
f=24:t=1/f:k=255/120:k=k/f
r=255:dc=atn(1)/45:rd2=rd22
for i=0 to 360 step t
x=x0+rd*cos(i*dc):y=y0+rd*sin(i*dc)
xx=x0+rd2*cos(i*dc):yy=y0+rd2*sin(i*dc)
if i>0 and i<60 then r=r0.5*k: g=g+1.5*k
if i>60 and i<120 then r=r1.5*k:g=g+0.5*k
if i>120 and i<180 then g=g0.5*k: b=b+1.5*k
if i>180 and i<240 then g=g1.5*k:b=b+0.5*k
if i>240 and i<300 then b=b0.5*k: r=r+1.5*k
if i>300 and i<360 then b=b1.5*k:r=r+0.5*k
if s=1 then goto[skip1]
#1.gb "discard;down"
#1.gb "size 2;color ";r;" ";g;" ";b
#1.gb "line ";x0;" ";y0;" ";x;" ";y
#1.gb "color white;size 4;line ";x0;" ";y0;" ";xx;" ";yy;";flush"
goto[skip2]
[skip1]
sktclr$(1)=using("###",r)+" "+using("###",g)+" "+using("###",b)
if abs(angi)<0.03 then exit for
[skip2]
next
end sub
Function atan2(y,x)
'atan2 is a function which determines the angle between points
'x1, y1 and x2, y2. The angle returned is in radians
'The angle returned is always in the range of
'PI to PI radians (180 to 180 degrees)
'==============================================================
'NOTE the position of Y and X arguments
'This keeps atan2 function same as other language versions
'==============================================================
If x = 0 Then
If y < 0 Then
atan2 = 1.5707963267948967
Else
atan2 = 1.5707963267948967
End If
Else
chk = atn(y/x)
If x < 0 Then
If y < 0 Then
chk = chk  3.1415926535897932
Else
chk = chk + 3.1415926535897932
End If
End If
atan2 = chk
End If
'thanks Andy Amaya
End Function
* it is gtk+ imitation but lacking many features compared to that. not all color combinations included only 2 color combinations taken 3 color combinations not considered . * code for selecting shades(triangle part) not written. thanks to andy amaya for providing simple yet powerful function atan2 to forum . * if I were not aware of that function certainly I would have knocked my head a few times (nay many times) to write a similar function to find the angle made by a line. * thanks to B+ for introducing me atan2 function. :D


Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #1 on: May 16^{th}, 2017, 09:22am » 

It looks like drawing circle is slow so then you click it tried to draw it again resulting in slow reaction in click?
Probably you can reuse once drawn circle with getbmp/drawbmp.


Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



Valentin
Full Member
member is offline
Posts: 130


Re: color picker
« Reply #2 on: May 17^{th}, 2017, 01:55am » 

If the data were displayed in a texteditor, this would allow them to be copied / pasted


Logged




bplus
Senior Member
member is offline
Gender:
Posts: 1213


Re: color picker
« Reply #3 on: May 17^{th}, 2017, 03:29am » 

I confess, I had/have many questions when I first saw this.
I think many would be cleared up if I knew what gtk+ was.
I bet tk stands for tool kit, g ?
+ stands for me ;))
Is this a Liberty thing?


Logged

B+



tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #4 on: May 17^{th}, 2017, 04:08am » 

looks like: http://gcolor2.sourceforge.net/
(GTK is "GIMP ToolKit" https://www.gtk.org/ https://en.wikipedia.org/wiki/GTK%2B If you have Gimp installed, you have this color picker)

« Last Edit: May 17^{th}, 2017, 04:10am by tsh73 » 
Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



zzz000abc
Full Member
member is offline
Gender:
Posts: 256


Re: color picker
« Reply #5 on: May 17^{th}, 2017, 08:02am » 

tsh, Quote:Probably you can reuse once drawn circle with getbmp/drawbmp. 

I will consider that in next version. But still didn't get how to make triangle part/and how to make it faster.
Valentin, Quote:If the data were displayed in a texteditor, this would allow them to be copied / pasted 

that part you can extend easily without changing the original code. even though I am providing it for you. Code: nomainwin
pi=3.14159265
ww=DisplayWidth/2
wh=DisplayHeight/2
WindowWidth=ww
WindowHeight=wh
graphicbox #1.gb 0,0,ww/2,ww/2
textbox #1.tb1 ww/2+10,0,100,30
textbox #1.tb2 ww/2+10+100+10,0,100,30
textbox #1.tb3 ww/2+10+200+20,0,100,30
texteditor #1.te1 ww/2+10,80,ww/3,50
button #1.bt1 "save",[save],UR,20,80
textbox #1.tb4 ww/2+10,150,300,40
textbox #1.tb5 ww/2+10,150+70,300,40
textbox #1.tb6 ww/2+10,150+140,300,40
open"JB color pick"for window as#1
#1 "trapclose [quit]"
dc=(atn(1))/45
xx=ww/4:yy=wh/320'centre
leg=ww/6:stk=25 'leg and stroke lengths
clr1$="blue":clr2$="white"
leg=100
call cring xx,yy,leg,0,0
leg=leg25
call try60 xx,yy,i,leg,stk,clr1$,sclr$
sktclr$(1)=" 255 0 0"
#1.tb1 "!font tahoma 12":#1.tb1 " red"
#1.tb2 "!font tahoma 12":#1.tb2 " green"
#1.tb3 "!font tahoma 12":#1.tb3 " blue"
#1.tb4 "!font tahoma 15"
#1.tb5 "!font tahoma 15"
#1.tb6 "!font tahoma 15"
#1.te1 "!font tahoma 15"
#1.gb "when leftButtonUp [mup] "
wait
[mup]
mx=MouseX:my=MouseY
Dy=yymy:Dx=mxxx
ang=atan2(Dy,Dx)
r1=Dx/cos(ang)
if ang<0 then ang=2*pi+ang
if r1>=leg and r1<=leg+stk then
sclr$=sktclr$(1)
call cring xx,yy,leg,1,(360ang/dc)
call try60 xx,yy,i,leg,stk,clr2$,sclr$
sclr$="black"
call try60 xx,yy,ang/dc,leg,stk,clr1$,sclr$
i=ang/dc
tbclr$=sktclr$(1)
r=int(val(word$(tbclr$,1)))
g=int(val(word$(tbclr$,2)))
b=int(val(word$(tbclr$,3)))
#1.gb "color ";abs(240r);" ";abs(240g);" ";abs(240b)
#1.gb "backcolor ";sktclr$(1)
#1.gb "up;goto 300 300;down;circlefilled 20;flush"
#1.gb "backcolor white"
#1.tb1 " ";r
#1.tb2 " ";g
#1.tb3 " ";b
#1.tb4 "color ";tbclr$
#1.tb5 "backcolor ";tbclr$
#1.tb6 "fill ";tbclr$
end if
wait
[save]
#1.te1 " ";r;" ";g;" ";b
wait
[quit]
close #1
end
sub try60 x0,y0,ang0,leg,stk,clr$,sclr$
dc=(atn(1))/45 :pi=4*atn(1)
rd1=(ang0)*dc
rd2=(ang0+60)*dc
rd3=(ang0+120)*dc
x1=x0+leg*cos(rd1)
y1=y0leg*sin(rd1)
x2=x0leg*cos(rd2)
y2=y0+leg*sin(rd2)
x3=x0+leg*cos(rd3)
y3=y0leg*sin(rd3)
x11=x0+(leg+stk)*cos(rd1)
y11=y0(leg+stk)*sin(rd1)
x22=x0+(leg+6)*cos(rd1)
y22=y0(leg+6)*sin(rd1)
#1.gb "discard;color ";clr$
#1.gb "down;size 2;line ";x1;" ";y1;" ";x2;" ";y2
#1.gb "line ";x1;" ";y1;" ";x3;" ";y3
#1.gb "line ";x2;" ";y2;" ";x3;" ";y3
if sclr$<>"" then #1.gb "size 3;color ";sclr$
#1.gb "line ";x11;" ";y11;" ";x22;" ";y22;"; flush"
#1.gb "color ";clr$
end sub
sub cring x0,y0,rd,s,ang
f=24:t=1/f:k=255/120:k=k/f
r=255:dc=atn(1)/45:rd2=rd22
for i=0 to 360 step t
x=x0+rd*cos(i*dc):y=y0+rd*sin(i*dc)
xx=x0+rd2*cos(i*dc):yy=y0+rd2*sin(i*dc)
if i>0 and i<60 then r=r0.5*k: g=g+1.5*k
if i>60 and i<120 then r=r1.5*k:g=g+0.5*k
if i>120 and i<180 then g=g0.5*k: b=b+1.5*k
if i>180 and i<240 then g=g1.5*k:b=b+0.5*k
if i>240 and i<300 then b=b0.5*k: r=r+1.5*k
if i>300 and i<360 then b=b1.5*k:r=r+0.5*k
if s=1 then goto[skip1]
#1.gb "discard;down"
#1.gb "size 2;color ";r;" ";g;" ";b
#1.gb "line ";x0;" ";y0;" ";x;" ";y
#1.gb "color white;size 4;line ";x0;" ";y0;" ";xx;" ";yy;";flush"
goto[skip2]
[skip1]
sktclr$(1)=using("###",r)+" "+using("###",g)+" "+using("###",b)
if abs(angi)<0.03 then exit for
[skip2]
next
end sub
Function atan2(y,x)
'atan2 is a function which determines the angle between points
'x1, y1 and x2, y2. The angle returned is in radians
'The angle returned is always in the range of
'PI to PI radians (180 to 180 degrees)
'==============================================================
'NOTE the position of Y and X arguments
'This keeps atan2 function same as other language versions
'==============================================================
If x = 0 Then
If y < 0 Then
atan2 = 1.5707963267948967
Else
atan2 = 1.5707963267948967
End If
Else
chk = atn(y/x)
If x < 0 Then
If y < 0 Then
chk = chk  3.1415926535897932
Else
chk = chk + 3.1415926535897932
End If
End If
atan2 = chk
End If
'thanks Andy Amaya
End Function
Edit1. If you want text editor bigger then quote all the lines with #1.tb in the code and and increase the height of text editor. for example you can write 150 or 300 in place of 50(text editor height in the code. See the line starting with text editor #1.te1)
B+, Quote:I think many would be cleared up if I knew what gtk+ was. 

tsh is right , it stands for 'Gimp tool kit' if you enjoy compiling on linux then download it from
http://gcolor2.sourceforge.net/ it works without Gimp also. If you have any linux you are more likely to find it preinstalled.
If you like to install Linux on cd/pendrive/memory card the send me a message so that i can send you a link and guide you how to install it on a memorycard/pen drive(it requires less than 300Mb)

« Last Edit: May 17^{th}, 2017, 08:21am by zzz000abc » 
Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #6 on: May 17^{th}, 2017, 09:45am » 

Went for triangle Upright standing, simple color (red). I have no idea about how it goes for other hews, (need to make/find somewhere HSL/HSV color) but for red it looks similar  and takes about 2 second on my machine just to do color changing/point setting (and there is no way around color changing!) Code:nomainwin
open "test" for graphics as #gr
#gr "down"
#gr "trapclose [quit]"
t0=time$("ms")
Jmax = 100*sqr(3)/2
for i = 1 to 100 'horiz
y=abs(i50)*sqr(3)
'#gr "set ";i;" ";y
cmd$ = ""
for j = y to Jmax 'vert, up is most saturated (255 0 0)
'#gr "color ";int(rnd(0)*255);" ";int(rnd(0)*255);" ";int(rnd(0)*255)
'#gr "set ";int(i);" ";int(j)
'for red color
'r is distance from lower left corner, here 0..100
'g, b from lower right
r= sqr(i^2+(jJmax)^2)
g= sqr((i100)^2+(jJmax)^2)
'but no more then r
g = g/100*r
'b is the same
b= g
'scale to 0..255
r = int(r/100*255)
g = int(g/100*255)
b = int(b/100*255)
'build long string
cmd$ = cmd$;";";"color ";r;" ";g;" ";b
cmd$ = cmd$;";";"set ";int(i);" ";int(j)
next
'print left$(cmd$, 1000)
#gr mid$(cmd$,2)
next
#gr "flush"
dt = time$("ms")t0
notice "dt"; dt '422 ms without color change, 2096 with random colors
wait
[quit]
close #gr
end
EDIT some discussion on HSL and RGB on LB forum RGB to HSL converter

« Last Edit: May 17^{th}, 2017, 09:56am by tsh73 » 
Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



bplus
Senior Member
member is offline
Gender:
Posts: 1213


Re: color picker
« Reply #7 on: May 17^{th}, 2017, 09:54am » 

Thanks tsh,
zzz000abc Quote:If you like to install Linux on cd/pendrive/memory card the send me a message so that i can send you a link and guide you how to install it on a memorycard/pen drive(it requires less than 300Mb) 

Oh Good BOT above!
I have ANOTHER Linux friend.
To get this joke, you have to know I don't have Linux (but I have looked into possibly getting it and overwhelmed by the choices and the tech talk, Yikes!)
But I keep making these friends who do cool stuff with BASIC and, I don't know, maybe 9 times out of 10 they use Linux.
zzz000abc, I might take you up on your offer, I like the flash drive idea. I am wearing out trying not to be taken by the conspiracy. "Resistance is futile.", they keep coming back like space invaders.

« Last Edit: May 17^{th}, 2017, 09:57am by bplus » 
Logged

B+



bplus
Senior Member
member is offline
Gender:
Posts: 1213


Re: color picker
« Reply #8 on: May 17^{th}, 2017, 10:14am » 

Ah, while thanking the great BOT above, IT sends another one of IT's angels.
tsh, I see another elegant solution in how the triangle might be worked into zzz's picker.
That was fast work, unless you were waiting for my comment to continue. I will check it out.


Logged

B+



tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #9 on: May 17^{th}, 2017, 3:25pm » 

zzz000abc, I indented your code so I can read it and speed color circle part about 20x * by decreasing "f" from 24 to 2 (parts single degree splitted by)  test shows that 1 leaves some gaps but 2 is enogh for smooth picture * and by removing part that clears part of line be overprinting it with white (just draw only part that needed).
Nothing else changed. Code:nomainwin
pi=3.14159265
ww=DisplayWidth/2
wh=DisplayHeight/2
WindowWidth=ww
WindowHeight=wh
graphicbox #1.gb 0,0,ww/2,ww/2
textbox #1.tb1 ww/2+10,0,100,30
textbox #1.tb2 ww/2+10+100+10,0,100,30
textbox #1.tb3 ww/2+10+200+20,0,100,30
texteditor #1.te1 ww/2+10,80,ww/3,50
button #1.bt1 "save",[save],UR,20,80
textbox #1.tb4 ww/2+10,150,300,40
textbox #1.tb5 ww/2+10,150+70,300,40
textbox #1.tb6 ww/2+10,150+140,300,40
open "JB color pick"for window as #1
#1 "trapclose [quit]"
dc=(atn(1))/45
xx=ww/4:yy=wh/320'centre
leg=ww/6:stk=25 'leg and stroke lengths
clr1$="blue":clr2$="white"
leg=100
t0=time$("ms")
call cring xx,yy,leg,0,0
t1=time$("ms")
'notice "cring time";t1t0 'src: 81xx
'decreased 10x by changing f from 24 to 2
'2x mode by removing white middle part
'(if needed, circlefilled will do that instantly)
leg=leg25
call try60 xx,yy,i,leg,stk,clr1$,sclr$
sktclr$(1)=" 255 0 0"
#1.tb1 "!font tahoma 12":#1.tb1 " red"
#1.tb2 "!font tahoma 12":#1.tb2 " green"
#1.tb3 "!font tahoma 12":#1.tb3 " blue"
#1.tb4 "!font tahoma 15"
#1.tb5 "!font tahoma 15"
#1.tb6 "!font tahoma 15"
#1.te1 "!font tahoma 15"
#1.gb "when leftButtonUp [mup] "
wait
[mup]
mx=MouseX:my=MouseY
Dy=yymy:Dx=mxxx
ang=atan2(Dy,Dx)
r1=Dx/cos(ang)
if ang<0 then ang=2*pi+ang
if r1>=leg and r1<=leg+stk then
sclr$=sktclr$(1)
call cring xx,yy,leg,1,(360ang/dc)
call try60 xx,yy,i,leg,stk,clr2$,sclr$
sclr$="black"
call try60 xx,yy,ang/dc,leg,stk,clr1$,sclr$
i=ang/dc
tbclr$=sktclr$(1)
r=int(val(word$(tbclr$,1)))
g=int(val(word$(tbclr$,2)))
b=int(val(word$(tbclr$,3)))
#1.gb "color ";abs(240r);" ";abs(240g);" ";abs(240b)
#1.gb "backcolor ";sktclr$(1)
#1.gb "up;goto 300 300;down;circlefilled 20;flush"
#1.gb "backcolor white"
#1.tb1 " ";r
#1.tb2 " ";g
#1.tb3 " ";b
#1.tb4 "color ";tbclr$
#1.tb5 "backcolor ";tbclr$
#1.tb6 "fill ";tbclr$
end if
wait
[save]
#1.te1 " ";r;" ";g;" ";b
wait
[quit]
close #1
end
sub try60 x0,y0,ang0,leg,stk,clr$,sclr$
dc=(atn(1))/45 :pi=4*atn(1)
rd1=(ang0)*dc
rd2=(ang0+60)*dc
rd3=(ang0+120)*dc
x1=x0+leg*cos(rd1)
y1=y0leg*sin(rd1)
x2=x0leg*cos(rd2)
y2=y0+leg*sin(rd2)
x3=x0+leg*cos(rd3)
y3=y0leg*sin(rd3)
x11=x0+(leg+stk)*cos(rd1)
y11=y0(leg+stk)*sin(rd1)
x22=x0+(leg+6)*cos(rd1)
y22=y0(leg+6)*sin(rd1)
#1.gb "discard;color ";clr$
#1.gb "down;size 2;line ";x1;" ";y1;" ";x2;" ";y2
#1.gb "line ";x1;" ";y1;" ";x3;" ";y3
#1.gb "line ";x2;" ";y2;" ";x3;" ";y3
if sclr$<>"" then #1.gb "size 3;color ";sclr$
#1.gb "line ";x11;" ";y11;" ";x22;" ";y22;"; flush"
#1.gb "color ";clr$
end sub
sub cring x0,y0,rd,s,ang
'f=24 '2 is enough, and 10x faster
f=2:t=1/f:k=255/120:k=k/f
r=255:dc=atn(1)/45:rd2=rd22
#1.gb "discard;down"
#1.gb "size 2"
for i=0 to 360 step t
x=x0+rd*cos(i*dc):y=y0+rd*sin(i*dc)
xx=x0+rd2*cos(i*dc):yy=y0+rd2*sin(i*dc)
if i>0 and i<60 then r=r0.5*k: g=g+1.5*k
if i>60 and i<120 then r=r1.5*k:g=g+0.5*k
if i>120 and i<180 then g=g0.5*k: b=b+1.5*k
if i>180 and i<240 then g=g1.5*k:b=b+0.5*k
if i>240 and i<300 then b=b0.5*k: r=r+1.5*k
if i>300 and i<360 then b=b1.5*k:r=r+0.5*k
if s=1 then goto[skip1]
#1.gb "color ";r;" ";g;" ";b
#1.gb "line ";xx;" ";yy;" ";x;" ";y
goto[skip2]
[skip1]
sktclr$(1)=using("###",r)+" "+using("###",g)+" "+using("###",b)
'if abs(angi)<0.03 then exit for
if abs(angi)<2/f then exit for
[skip2]
next
#1.gb "flush"
end sub
Function atan2(y,x)
'atan2 is a function which determines the angle between points
'x1, y1 and x2, y2. The angle returned is in radians
'The angle returned is always in the range of
'PI to PI radians (180 to 180 degrees)
'==============================================================
'NOTE the position of Y and X arguments
'This keeps atan2 function same as other language versions
'==============================================================
If x = 0 Then
If y < 0 Then
atan2 = 1.5707963267948967
Else
atan2 = 1.5707963267948967
End If
Else
chk = atn(y/x)
If x < 0 Then
If y < 0 Then
chk = chk  3.1415926535897932
Else
chk = chk + 3.1415926535897932
End If
End If
atan2 = chk
End If
'thanks Andy Amaya
End Function


Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #10 on: May 17^{th}, 2017, 3:52pm » 

Found old code of mine doing HSV_2_RGB Inserted it into color triangle  looks good Takes about same amount of time (2 sec)
(as to how make rotated triangle, I have no idea ) Code:nomainwin
'random hew
h = int(rnd(0)*360)
'h=0 'for red
open "Random hew=";h for graphics as #gr
#gr "down"
#gr "trapclose [quit]"
t0=time$("ms")
Jmax = 100*sqr(3)/2
for i = 1 to 100 'horiz
SCAN
y=int(abs(i50)*sqr(3)) 'prevents jagged bottom
'#gr "set ";i;" ";y
cmd$ = ""
for j = y to Jmax 'vert, up is most saturated (255 0 0)
'#gr "color ";int(rnd(0)*255);" ";int(rnd(0)*255);" ";int(rnd(0)*255)
'#gr "set ";int(i);" ";int(j)
's is distance from lower right corner, here 0..100
'v from lower left
'should be 0..1
s= sqr((i100)^2+(jJmax)^2)/100
v= sqr(i^2+(jJmax)^2)/100
gosub [HSV_2_RGB]
'build long string  no speed up for JB/LB (but 1.5x faster for LBB)
'cmd$ = cmd$;";";"color ";RGB$
'cmd$ = cmd$;";";"set ";int(i);" ";int(j)
#gr "color ";RGB$
#gr "set ";int(i);" ";int(j)
next
'print left$(cmd$, 1000)
'#gr mid$(cmd$,2)
next
#gr "flush"
dt = time$("ms")t0
notice "Time taken";chr$(13); dt;" ms" '422 ms without color change, 2096 with random colors
wait
[quit]
close #gr
end
[HSV_2_RGB]
'ref from Wikipedia
'http://en.wikipedia.org/wiki/HSL_and_HSV
'Input: (h,s,v)
'h in the range [0, 360), indicating the angle, in degrees of the hue
's and v varying between 0 and 1, representing the saturation and value, respectively
'Output: r,g,b [0,1]
'and to be useful, R G B [0 255]
'or to JB RGB$ as "R G B" string.
hi = int(h/60) mod 6
f = h/60  int(h/60)
p = v*(1s)
q= v*(1f*s)
t = v*(1(1f)*s)
' print hi,
select case hi
case 0
r = v: g = t: b = p
case 1
r = q: g = v: b = p
case 2
r = p: g = v: b = t
case 3
r = p: g = q: b = v
case 4
r = t: g = p: b = v
case 5
r = v: g = p: b = q
end select
R = int(r*255)
G = int(g*255)
B = int(b*255)
RGB$= R;" ";G;" ";B
return


Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



zzz000abc
Full Member
member is offline
Gender:
Posts: 256


Re: color picker
« Reply #11 on: May 18^{th}, 2017, 12:32am » 

Quote:and speed color circle part about 20x * by decreasing "f" from 24 to 2 (parts single degree splitted by)  test shows that 1 leaves some gaps but 2 is enogh for smooth picture 

thank you tsh, for your swift moves. that part I have taken from my code for color circle as it is. I made the fine divisions (f=24)to maximize the color variation there and it kept it aside to work on it again later.
Quote:I have no idea about how it goes for other hews, (need to make/find somewhere HSL/HSV color) 

I went through wiki on hsv and after that again ran gtk program learnt some thing. here are my observations: we have three colors combination always say A,B,C with variable values. I call most dominating one(with largest value among three) A ,less dominant C ,remaining one B. we have a traingle with 3 vertices the one with pointer I call CV,bright one BV,dark one DV and the small circle movable over triangle cp. now keeping cp over CV and rotate the traingle you find Saturation always 100%.Hue is angle described by CV.when cp is BV Value is 100% and when it is on DV it is 0. with cp on CV when trinagle is rotated you can notice always the color C is zero.AB change their values. to increase Value(to brighten) increase the value of C but it should not go over B .to darken decrease the value of AB but it should not under C. so 1.C is zero saturataion 100%. C is non zero it less than 100%. 2.when A=B=C=255 Value is 100%. When A=B=C=0 Value is 0. 3.Hue is simply angle made by the triangle.
Quote:(as to how make rotated triangle, I have no idea )


we have an equilateral (equiangular triangle),having same centre of the circle. being symmetric the lines drawn to it's vertices from the centre(say r1,r2,r3; r1=r2=r3) trisect the circle. if r1,makes x degrees the remaing two make x+60 and x+120. just run the loop from x=0 to 360 to rotate. the leg in my code actually not the leg of the triangle but refers to radius ( proportional to leg)

« Last Edit: May 18^{th}, 2017, 12:34am by zzz000abc » 
Logged




zzz000abc
Full Member
member is offline
Gender:
Posts: 256


Re: color picker
« Reply #12 on: May 18^{th}, 2017, 06:48am » 

here is version 2A. Code: nomainwin
pi=3.14159265:ex=0
ww=DisplayWidth/2
wh=DisplayHeight/2
WindowWidth=ww
WindowHeight=wh
graphicbox #1.gb 0,0,ww/2,ww/2
textbox #1.tb1 ww/2+10,0,100,30
textbox #1.tb2 ww/2+10+100+10,0,100,30
textbox #1.tb3 ww/2+10+200+20,0,100,30
texteditor #1.te1 ww/2+10,80,ww/3,50
button #1.bt1 "save",[save],UR,20,80
textbox #1.tb4 ww/2+10,150,300,40
textbox #1.tb5 ww/2+10,150+70,300,40
textbox #1.tb6 ww/2+10,150+140,300,40
open"JB color pick"for window as#1
#1 "trapclose [quit]"
dc=(atn(1))/45
xx=ww/4:yy=wh/320'centre
leg=ww/6:stk=25 'leg and stroke lengths
clr1$="blue":clr2$="white"
leg=100
call cring xx,yy,leg,0,0
leg=leg25
call try60 xx,yy,i,leg,stk,clr1$,sclr$
sktclr$(1)=" 255 0 0"
#1.tb1 "!font tahoma 12":#1.tb1 " red"
#1.tb2 "!font tahoma 12":#1.tb2 " green"
#1.tb3 "!font tahoma 12":#1.tb3 " blue"
#1.tb4 "!font tahoma 15"
#1.tb5 "!font tahoma 15"
#1.tb6 "!font tahoma 15"
#1.te1 "!font tahoma 15"
#1.gb "when leftButtonUp [mup] "
wait
[mup]
mx=MouseX:my=MouseY
Dy=yymy:Dx=mxxx
ang=atan2(Dy,Dx)
r1=Dx/cos(ang)
if ang<0 then ang=2*pi+ang
if r1>=leg and r1<=leg+stk then
sclr$=sktclr$(1)
call cring xx,yy,leg,1,(360ang/dc)
call try60 xx,yy,i,leg,stk,clr2$,sclr$
sclr$="black"
call try60 xx,yy,ang/dc,leg,stk,clr1$,sclr$
i=ang/dc
tbclr$=sktclr$(1)
end if
r=int(val(word$(tbclr$,1)))
g=int(val(word$(tbclr$,2)))
b=int(val(word$(tbclr$,3)))
mf=255/max(r,g,b)
rb=mf*r:gb=mf*g:bb=mf*b:rd=r/2:gd=g/2:bd=b/2
#1.gb "color ";abs(240r);" ";abs(240g);" ";abs(240b)
#1.gb "backcolor ";rb;" ";gb;" ";bb
#1.gb "up;goto 80 300;down;circlefilled 20;flush"
#1.gb "backcolor ";r;" ";g;" ";b
#1.gb "up;goto 180 290;down;circlefilled 40;flush"
#1.gb "backcolor ";rd;" ";gd;" ";bd
#1.gb "up;goto 280 300;down;circlefilled 20;flush"
#1.gb "backcolor white"
if 60<mx and mx<100 and 280<my and my<320 then r=rb:g=gb:b=bb
if 260<mx and mx<300 and 280<my and my<320 then r=rd:g=gd:b=bd
r=int(r):g=int(g):b=int(b)
#1.tb1 " ";r
#1.tb2 " ";g
#1.tb3 " ";b
#1.tb4 "color ";r;" ";g;" ";b
#1.tb5 "backcolor ";r;" ";g;" ";b
#1.tb6 "fill ";r;" ";g;" ";b
'end if
wait
[save]
#1.te1 " ";r;" ";g;" ";b
wait
[quit]
close #1
end
sub try60 x0,y0,ang0,leg,stk,clr$,sclr$
dc=(atn(1))/45 :pi=4*atn(1)
rd1=(ang0)*dc
rd2=(ang0+60)*dc
rd3=(ang0+120)*dc
x1=x0+leg*cos(rd1)
y1=y0leg*sin(rd1)
x2=x0leg*cos(rd2)
y2=y0+leg*sin(rd2)
x3=x0+leg*cos(rd3)
y3=y0leg*sin(rd3)
x11=x0+(leg+stk)*cos(rd1)
y11=y0(leg+stk)*sin(rd1)
x22=x0+(leg+6)*cos(rd1)
y22=y0(leg+6)*sin(rd1)
#1.gb "discard;color ";clr$
#1.gb "down;size 2;line ";x1;" ";y1;" ";x2;" ";y2
#1.gb "line ";x1;" ";y1;" ";x3;" ";y3
#1.gb "line ";x2;" ";y2;" ";x3;" ";y3
if sclr$<>"" then #1.gb "size 3;color ";sclr$
#1.gb "line ";x11;" ";y11;" ";x22;" ";y22;"; flush"
#1.gb "color ";clr$
end sub
sub cring x0,y0,rd,s,ang
f=24:t=1/f:k=255/120:k=k/f
r=255:dc=atn(1)/45:rd2=rd22
for i=0 to 360 step t
x=x0+rd*cos(i*dc):y=y0+rd*sin(i*dc)
xx=x0+rd2*cos(i*dc):yy=y0+rd2*sin(i*dc)
if i>0 and i<60 then r=r0.5*k: g=g+1.5*k
if i>60 and i<120 then r=r1.5*k:g=g+0.5*k
if i>120 and i<180 then g=g0.5*k: b=b+1.5*k
if i>180 and i<240 then g=g1.5*k:b=b+0.5*k
if i>240 and i<300 then b=b0.5*k: r=r+1.5*k
if i>300 and i<360 then b=b1.5*k:r=r+0.5*k
if s=1 then goto[skip1]
#1.gb "discard;down"
#1.gb "size 2;color ";r;" ";g;" ";b
#1.gb "line ";x0;" ";y0;" ";x;" ";y
#1.gb "color white;size 4;line ";x0;" ";y0;" ";xx;" ";yy;";flush"
goto[skip2]
[skip1]
sktclr$(1)=using("###",r)+" "+using("###",g)+" "+using("###",b)
if abs(angi)<0.03 then exit for
[skip2]
next
end sub
Function atan2(y,x)
'atan2 is a function which determines the angle between points
'x1, y1 and x2, y2. The angle returned is in radians
'The angle returned is always in the range of
'PI to PI radians (180 to 180 degrees)
'==============================================================
'NOTE the position of Y and X arguments
'This keeps atan2 function same as other language versions
'==============================================================
If x = 0 Then
If y < 0 Then
atan2 = 1.5707963267948967
Else
atan2 = 1.5707963267948967
End If
Else
chk = atn(y/x)
If x < 0 Then
If y < 0 Then
chk = chk  3.1415926535897932
Else
chk = chk + 3.1415926535897932
End If
End If
atan2 = chk
End If
'thanks Andy Amaya
End Function
function max(rr,gg,bb)
if rr>=gg and rr>=bb then mm=rr
if gg>=rr and gg>=bb then mm=gg
if bb>=rr and bb>=gg then mm=bb
max=mm
end function
click on color ring to select a color. click one of the circles to select shade.
Edit1. a note about version . for shading part I thought of 3 ways.A.using circles as shown in the above code. B.using a second color ring. C.gtk triangle method. so here 2A means(atleast for me) version 2 following method A for shading.

« Last Edit: May 18^{th}, 2017, 07:02am by zzz000abc » 
Logged




tsh73
JBSupporter
member is offline
Gender:
Posts: 3619


Re: color picker
« Reply #13 on: May 18^{th}, 2017, 3:46pm » 

Ok I made shaded triangle rotate I used size 2 points to speed it up
Code:nomainwin
sq32=sqr(3)/2
pi = acs(1)
open "Any hue" for graphics as #gr
#gr "down"
#gr "trapclose [quit]"
#gr "size 2" 'size 2: to speed up
cx=150
cy=100+50/sq32
Jmax = 100*sq32
for h = 0 to 360
'color circle
a=0h*pi/180
cc=cos(a)
ss=sin(a)
s=1:v=1
gosub [HSV_2_RGB]
#gr "color ";RGB$
#gr "line ";cx+cc*rr;" ";cy+ss*rr;" ";cx+cc*(rr+12);" ";cy+ss*(rr+12)
if h mod 30 <>0 then goto [skip1] 'only every 30 degrees
'color triangle
'clear old triangle
#gr "place ";cx;" ";cy
rr=int(50/sq32)
#gr "color white"
#gr "circlefilled ";rr
'draw new one
a=0h*pi/180+pi/2
cc=cos(a)
ss=sin(a)
for i = 0rr to rr step 2 'step 2: size 2: to speed up
for j = 0rr to rr step 2 '//
'rotate and see if it goes into triangle
x=i+cx
y=j+cy
gosub [rotate] 'rotate backwards
'check if rotated point is in triangle
if (abs(xncx)<=50) and (yn100 <= Jmax) and (yn100 >= int(abs(xncx)*sqr(3))) then
'then calculate color by xn yn and plot it
s= sqr(((xncx)50)^2+(yn100Jmax)^2)/100
v= sqr(((xncx)+50)^2+(yn100Jmax)^2)/100
gosub [HSV_2_RGB]
#gr "color ";RGB$
#gr "set ";int(x);" ";int(y)
end if
next
next
#gr "flush"
timer 1000, [www] 'wait a second
wait
[www] timer 0
[skip1]
next
wait
[quit]
close #gr
end
[HSV_2_RGB]
'ref from Wikipedia
'http://en.wikipedia.org/wiki/HSL_and_HSV
'Input: (h,s,v)
'h in the range [0, 360), indicating the angle, in degrees of the hue
's and v varying between 0 and 1, representing the saturation and value, respectively
'Output: r,g,b [0,1]
'and to be useful, R G B [0 255]
'or to JB RGB$ as "R G B" string.
hi = int(h/60) mod 6
f = h/60  int(h/60)
p = v*(1s)
q= v*(1f*s)
t = v*(1(1f)*s)
' print hi,
select case hi
case 0
r = v: g = t: b = p
case 1
r = q: g = v: b = p
case 2
r = p: g = v: b = t
case 3
r = p: g = q: b = v
case 4
r = t: g = p: b = v
case 5
r = v: g = p: b = q
end select
R = int(r*255)
G = int(g*255)
B = int(b*255)
RGB$= R;" ";G;" ";B
return
[rotate]
'rot (x,y) by a around (cx, cy)
'cc=cos(a)
'ss=sin(a)
xn=(xcx)*cc+(ycy)*ss+cx
yn=0((xcx)*ss(ycy)*cc)+cy
return


Logged

Q: "And if I took your codes and compile them, and sell them for a profit"? A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it  please do. (enjoying JB 1.01 on WinXP, netbook and desktop)



