a small colormap Gadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

a small colormap Gadget

Post by dobro »

2 functions is needed

for drawing a gadget : Palette_gadget(num,x,y,cadre)
ps: "Cadre" is a frame around the Gadget (0 or 1 )

one for the management of events has put into the event loop : event_palette_gadget(num)

Here is the code example ... colorful thing in the middle is the gadget: mrgreen:
a click on it, it return the selected color :)


made good use :)
Image

the code :

Code: Select all

;***********************************************
;Titre  :*Palette_gadget
;Auteur  : Dobro 
; Collaboration : Ar-s
;Date  :15/06/2014
;Heure  :18:46:29
;Version Purebasic :  PureBasic 5.22 LTS (Windows - x86)
;Version de l'editeur :EPB V2.54
; Libairies necessaire : Aucune 
;***********************************************

;}

Declare.l event_palette_gadget(num) ; a Mettre en Boucle principal  
Declare  Palette_gadget(num,x,y,cadre=0,titre$=""); a Mettre apres Openwindow()

;{- ***** initialisation du gadget Palette ******************
Structure Pal  ; initialisation du gadget palette
	Couleur_hex.s ; pour la couleur Hexa
	Couleur.i ; la couleur
	Rouge.i ; composante rouge
	Vert.i ; composante Verte
	Bleu.i ;composante bleu
EndStructure
Global Dim Pal.Pal(10) ; valable pour 10 gadget palettes
Global Fnt=LoadFont (#Pb_any, "Arial", 6)
;} ****************************************************


Enumeration
	#palette
	#palette2
EndEnumeration



If OpenWindow(0, 0, 0, 800, 600, "Pick_color", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	Palette_gadget(#palette,20,10,0)   ; dessin du gadget palette sans cadre
	Palette_gadget(#palette2,20,300,1,"Palette By Dobro")   ; dessin du gadget palette Avec cadre
	Repeat
		Event = WaitWindowEvent(2)
		Select Event
			Case #PB_Event_Gadget
			select EventGadget() 
				case #palette
				retour=event_palette_gadget(#palette) ; envoi l'event au gadget Palette , qui retourne la couleur choisie dans la Structure
				if retour<>-1
					debug "couleur Palette 1="+ Pal(#palette)\couleur.i;  affiche la couleur  de la Structure
					debug "composante Palette 1= "+"Rouge "+Pal(#palette)\rouge.i+" Vert "+Pal(#palette)\vert.i+" Bleu "+Pal(#palette)\bleu.i ; retourne les composantes de la couleur choisie, present dans la structure
					Debug "retour de la couleur en Hexa :" +Pal(#palette)\Couleur_hex.s 
					Debug " ************************************************************************"
				Endif
				case #palette2
				
				retour=event_palette_gadget(#palette2) ;  envoi l'event au gadget Palette , qui retourne la couleur choisie dans la Structure
				if retour<>-1
					debug "couleur Palette 2="+ Pal(#palette2)\couleur.i; affiche la couleur  de la Structure
					debug "composante Palette 2= "+"Rouge "+Pal(#palette2)\rouge.i+" Vert "+Pal(#palette2)\vert.i+" Bleu "+Pal(#palette2)\bleu.i  ; affiche les composantes de la couleur choisie,  present dans la structure
					Debug "retour de la couleur en Hexa :" +Pal(#palette2)\Couleur_hex.s 
					Debug " ************************************************************************"
				Endif
				
			Endselect
		Endselect
	Until Event = #PB_Event_CloseWindow
Endif





;-** Zone Procedure ***************
Procedure.l event_palette_gadget(num) ; event_palette_gadget(numero du gadget)
	;By Dobro modif d'Ar-S
	; retourne la couleur choisie
	Static Flag_c ,mem_Flag_c
	Select EventType()
		Case #PB_EventType_MouseEnter, #PB_EventType_MouseMove
		Capture = 1
		x1 = GetGadgetAttribute(num, #PB_Canvas_MouseX)
		y1 = GetGadgetAttribute(num, #PB_Canvas_MouseY)
		If StartDrawing(CanvasOutput(num))
			If (x1 >= 0 And x1 <=255)  And (y1 >=0 And y1 <=275)
				Pal(num)\Couleur.i = Point(x1,y1)
				Box (257,0,20,128,Pal(num)\Couleur.i)
				Flag_c=Pal(num)\couleur.i
				if mem_Flag_c<>Flag_c
					h$=RSet (Hex(Red(Pal(num)\couleur.i)),2,"0") + RSet (Hex(Green(Pal(num)\couleur.i)),2,"0") + RSet (Hex(Blue(Pal(num)\couleur.i)),2,"0")
					DrawingFont(FontID(Fnt))      
					DrawText( 160,256, Space(32) )
					DrawText( 160,256, "Hex : "+ h$ )
					DrawText( 160,266, Space(32) )
					DrawText( 160,266, "R : "+Red(Pal(num)\couleur.i)+" V:"+Green(Pal(num)\couleur.i)+" B:"+Blue(Pal(num)\couleur.i) )
					
					mem_Flag_c=Flag_c
				Endif
			EndIf
			StopDrawing()
			;ProcedureReturn
		EndIf
		
		Case  #PB_EventType_LeftClick
		Capture = 0
		;-***** pointage souris ************
		x1 = GetGadgetAttribute(num, #PB_Canvas_MouseX)
		y1 = GetGadgetAttribute(num, #PB_Canvas_MouseY)
		; ********************************
		If StartDrawing(CanvasOutput(num))
			If (x1 >= 0 And x1 <=255)  And (y1 >=0 And y1 <=275)
				Pal(num)\Couleur.i = Point(x1,y1)
				Box (257,129,20,128,Pal(num)\Couleur.i)
				; met les composantes dans la structure
				Pal(num)\rouge.i=Red(Pal(num)\Couleur.i)
				Pal(num)\vert.i=Green(Pal(num)\Couleur.i)
				Pal(num)\bleu.i=Blue(Pal(num)\Couleur.i)
				; //Ajout  Ar-S 21/06/14 - 17h06//
				Pal(num)\Couleur_hex.s = RSet (Hex(Red(Pal(num)\couleur.i)),2,"0") + RSet (Hex(Green(Pal(num)\couleur.i)),2,"0") + RSet (Hex(Blue(Pal(num)\couleur.i)),2,"0") ; met la couleur Hex dans la structure
			EndIf
			StopDrawing()
			ProcedureReturn 
		EndIf
		Default
		Capture = 0
	EndSelect 
	ProcedureReturn -1
EndProcedure

Procedure Palette_gadget(num,x,y,cadre=0,titre$="");Palette_gadget(numero du gadget,x,y,largeur,hauteur)
	; By Dobro
	; Affiche le Gadget Palette
	Larg=276 :haut=276
	
	if cadre=1
		FrameGadget(#PB_Any, x-5, y-15, Larg+15, Haut+20, titre$)
	Endif
	CanvasGadget(num, x,y,larg,haut)
	If StartDrawing(CanvasOutput(num)) 
		
		;{ Palette Dobro
		; rouge=0
		; vert=0
		; bleu=0
		; DrawingMode(#PB_2DDrawing_Gradient)      
		; BackColor(rgb(0,0,0))
		; for y=0 to 255 
		; ;for x=0 to 255 
		; GradientColor(0.1, rgb(0,0,0)) ; rouge
		; GradientColor(0.2, rgb(y,0,0)) ; rouge
		; GradientColor(0.3, rgb(y,y,0)) ; jaune
		; GradientColor(0.4, rgb(0,y,0)) ; vert
		; GradientColor(0.5, rgb(0,y,y)) ; cyan
		; GradientColor(0.6, rgb(0,0,y)) ; bleu
		; GradientColor(0.7, rgb(y,0,y)) ; violet
		; GradientColor(0.8, rgb(y,0,0)) ; rouge
		; GradientColor(0.9, rgb(0,0,0)) ; noir
		; GradientColor(1.0, rgb(255,255,255)) ; blanc
		; LinearGradient(0, 0, 255, 0)    
		; box(0, y, 255,y)   
		; 
		; ;Next x
		; Next y
		; FrontColor(rgb(255,255,255))
		;}
		;{ Palette Graph100
		For y=0 to 255
			For x=0 to 255
				
				abscisse.f=x:Ordonne.f=y: max.f=256: maxY.f=256
				Protected couleur, taux.f, index1.f, index2.f, index3.f, index4.f, index5.f
				
				index1.f = max / 6
				index2.f = 2 * max / 6
				index3.f = 3 * max / 6
				index4.f = 4 * max / 6
				index5.f = 5 * max / 6
				
				If Ordonne * 2 / maxY > 1
					abscisse - index1
					If abscisse < 0 : abscisse + max : EndIf
				EndIf
				
				If abscisse >= 0 And abscisse <= index1
					couleur = RGB(255, (((abscisse * max) / index1) * 255) / max, 0)
				EndIf
				If abscisse > index1 And abscisse <= index2
					couleur = RGB(((max - ((abscisse - index1) * max) / index1) * 255) / max, 255, 0)
				EndIf
				If abscisse > index2 And abscisse <= index3
					couleur = RGB(0, 255, ((((abscisse - index2) * max) / index1) * 255) / max)
				EndIf
				If abscisse > index3 And abscisse <= index4
					couleur = RGB(0, ((max - ((abscisse - index3) * max) / index1) * 255) / max, 255)
				EndIf
				If abscisse > index4 And abscisse <= index5
					couleur = RGB(((abscisse - index4) * 255) / index1, 0, 255)
				EndIf
				If abscisse > index5 And abscisse <= max
					couleur = RGB(255, 0, ((max - ((abscisse - index5) * max) / index1) * 255) / max)
				EndIf
				
				taux.f = Ordonne * 2 / maxY
				If taux <= 1
					couleur = RGB(taux * Red(couleur), taux * Green(couleur), taux * Blue(couleur))
					Else
					taux - 2
					couleur = RGB(255 + taux * Green(couleur), 255 + taux * Blue(couleur), 255 + taux * Red(couleur))
				EndIf
				x1=x+1
				y1=y+1
				if x1>255:x1=255:endif
				if y1>255:y1=255:endif
				plot(x1,y1,couleur)
				
			Next x
		Next y
		xb=0
		Box(xb,256,20,20,rgb(0,0,0))
		xb=xb+20
		Box(xb,256,20,20,rgb(255,255,255))
		xb=xb+20
		Box(xb,256,20,20,rgb(255,0,0))
		xb=xb+20
		Box(xb,256,20,20,rgb(255,255,0))
		xb=xb+20
		Box(xb,256,20,20,rgb(0,255,0))
		xb=xb+20
		Box(xb,256,20,20,rgb(0,255,255))
		xb=xb+20
		Box(xb,256,20,20,rgb(0,0,255))
		xb=xb+20
		Box(xb,256,20,20,rgb(255,0,255))
		
		;}
		StopDrawing()
		
	EndIf
Endprocedure



; 

; Epb

Last edited by dobro on Sun Jun 22, 2014 10:11 am, edited 23 times in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

I fixed a small bug :oops:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

I changed the code :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: a small colormap Gadget

Post by Fred »

You can edit your own post instead of creating a new one :)
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: a small colormap Gadget

Post by electrochrisso »

Not bad dobro, small code and to the point, I like it. :)
PureBasic! Purely the best 8)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

Thanks :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

Code updated
use of color generation has Graph100 :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

adding pure color (black, white, red, yellow, green, cyan, blue, purple)
adding a witness, color choice
Adding of optional parameters "cadre_" ("Framework") and "titre_de_cadre" (as a framework) :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: a small colormap Gadget

Post by dobro »

Adding : a Structure

and information in the Gadget :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: a small colormap Gadget

Post by davido »

I like it. Thank you for sharing. :D
DE AA EB
Post Reply