Page 1 sur 1

Retour colorpicker

Publié : lun. 27/mai/2019 0:08
par Ekim
Bonsoirs à tous

Existerait-il une methode, une API, bref un truc du genre "ColorPicker" pour soumettre une des valeurs TSL ou autres puis elle nous reverrais en retour une valeur RGB correspondante?

Re: Retour colorpicker

Publié : lun. 27/mai/2019 18:31
par case
des algo ici a convertir en pure basic eventuellement :)

https://www.cs.rit.edu/~ncs/color/t_convert.html

Re: Retour colorpicker

Publié : jeu. 04/juil./2019 23:50
par Ekim
Merci @Case
:wink:

Re: Retour colorpicker

Publié : ven. 05/juil./2019 8:25
par Mesa
J'ai retrouvé ça dans mes archives:

Code : Tout sélectionner

Procedure.l HSLToRGB(hue.a, saturation.a, lightness.a, alpha=0)
		Protected.f h=hue *6/256
		Protected.f s=saturation/255
		Protected.f l=lightness/255
		Protected.f c,x,r_,v_,b_,m
		c=(1-Abs(2*l-1))*s
		x=c*(1-Abs(Mod(h, 2) -1))
		Select Int(h)
				Case 0:r_=c:v_=x
				Case 1:r_=x:v_=c
				Case 2:v_=c:b_=x
				Case 3:v_=x:b_=c
				Case 4:r_=x:b_=c
				Case 5:r_=c:b_=x
		EndSelect
		m=l-c/2
		Protected r,v,b
		r=Int((r_+m)*255)
		v=Int((v_+m)*255)
		b=Int((b_+m)*255)
		ProcedureReturn RGBA(r,v,b,alpha)
EndProcedure

Procedure.l RGBToHSL(red.a, green.a, blue.a, alpha=0)
		Protected.f r=red/255
		Protected.f g=green/255
		Protected.f b=blue/255
		Protected.f cmax, cmin, h_,c,m,h,s,l
		Protected.l imax,imin
		If r>=g And r>=b:imax=1:cmax=r:EndIf
		If g>=r And g>=b:imax=2:cmax=g:EndIf
		If b>=r And b>=g:imax=3:cmax=b:EndIf
		If r<=g And r<=b:imin=1:cmin=r:EndIf
		If g<=r And g<=b:imin=2:cmin=g:EndIf
		If b<=r And b<=g:imin=3:cmin=b:EndIf
		c=cmax-cmin
		Select imax
				Case 1:h_=Mod((g-b)/c+6,6)
				Case 2:h_=(b-r)/c+2
				Case 3:h_=(r-g)/c+4
		EndSelect
		h=h_/6
		l=(cmax+cmin)/2
		If l<>1:s=c/(1-Abs(2*l-1)):Else:s=0:EndIf
		ProcedureReturn RGBA(h*255,s*255,l *255,alpha)  
EndProcedure

Procedure ColorBlend(color1.l, color2.l, blend.f)
		Protected r.w,g.w,b.w,a.w
		r=  Red(color1) + (Red(color2)     - Red(color1)) * blend
		g=Green(color1) + (Green(color2) - Green(color1)) * blend
		b= Blue(color1) + (Blue(color2) -   Blue(color1)) * blend
		a=Alpha(color1) + (Alpha(color2) - Alpha(color1)) * blend
		ProcedureReturn  RGBA(r,g,b,a)
EndProcedure

; ============= exemple ==============

Define.l i,j,n,x,y,r,h,c

OpenWindow(0, 0, 0, 512, 256, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CreateImage(0, 512, 256,24,$888888)
StartDrawing(ImageOutput(0))
Box(0, 0, 256, 256, $ffffff)
For j=0 To 255
		For i = 0 To 255
				Plot(i,j, HSLToRGB(i,j,128))
		Next
Next

DrawingMode(#PB_2DDrawing_Gradient)
Macro sphere
		ResetGradientColors()
		GradientColor(0.0,HSLToRGB(h,64,200))
		GradientColor(0.3,HSLToRGB(h,64,128))
		GradientColor(1.0,HSLToRGB(h,64,64))
		CircularGradient(x-r*0.3, y+r*1.2+r*0.5,r)    
		Circle(x,y+r*1.2,r)
		
		ResetGradientColors()
		GradientColor(0.0,HSLToRGB(h,255,255))
		GradientColor(0.3,HSLToRGB(h,255,128))
		GradientColor(1.0,HSLToRGB(h,255,64))
		CircularGradient(x-r*0.3, y-r*0.3,r)    
		Circle(x,y,r)
EndMacro

For j=1 To 4
		n=1<<j
		For i=0 To n-1
				h=(0.5+i)/n*256
				r=50/Sqr(n)
				r=120/n
				x=256+h
				y=16*(i & 1)+330-420/Sqr(n)
				sphere
		Next
Next
StopDrawing() 
ImageGadget(0, 0, 0, 200, 200, ImageID(0))

c=HSLToRGB(128,186,137)
Debug ""+Red(c)+"  "+Green(c)+"  "+Blue(c)+"  "+Alpha(c)
c=RGBToHSL(Red(c),Green(c),Blue(c))
Debug ""+Red(c)+"  "+Green(c)+"  "+Blue(c)+"  "+Alpha(c)

c=ColorBlend($ff0000,$0000ff,0.5)
Debug ""+Red(c)+"  "+Green(c)+"  "+Blue(c)+"  "+Alpha(c)

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

M.