PixelToy

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

PixelToy

Post by firace »

Just a toy. With pixels. Have fun and post your masterpieces!!!

Image

Notes:
- There are no special commands or settings. Just use your mouse to select colors and draw!
- Color palette is randomly generated
- Example in the screenshot was found on twitter but I can't find the author's name
- Uses some helper code by griz

Code: Select all

Procedure GetMouseX(gadget)    ;;  by griz
  GetCursorPos_(mouse.POINT) 
  MapWindowPoints_(0,GadgetID(gadget),mouse,1) 
  ProcedureReturn mouse\x 
EndProcedure 

Procedure GetMouseY(gadget) 
  GetCursorPos_(mouse.POINT) 
  MapWindowPoints_(0,GadgetID(gadget),mouse,1) 
  ProcedureReturn mouse\y 
EndProcedure 


#CELLSIZE = 16
#XXX = 16
#YYY = 20

AreaX  = #CELLSIZE * #XXX
AreaY  = #CELLSIZE * #YYY


OpenWindow( 0,0,0,AreaX+160,AreaY+2,"PixelToy " + #XXX + "x" + #YYY ,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 
SetWindowColor (0,0)

hImg = CreateImage(0,AreaX,AreaY) ; ,24,#White) 

ImageGadget(2,2,2,AreaX,AreaY,hImg)  

;; init palette 
CanvasGadget(3,AreaX + 50 , 4, 100 ,AreaY - 8)  
StartDrawing(CanvasOutput(3))

Box(0,0, 100 , AreaY , $191919)

For gg = 0 To 8
  R = Random(255)
  G = Random(255)
  B = Random(255)
  color = (RGB(R, G, B))
  Box(20, 18 + gg*30 , 20, 20, color)
Next 

For gg = 0 To 8
  R = Random(255)
  G = Random(255)
  B = Random(255)
  color = (RGB(R, G, B))
  Box(50, 18 + gg*30 , 20, 20, color)
Next 

selectedColor = color 
Box(1, 1, 3, WindowHeight(0), selectedColor)

StopDrawing()

;; init palette end 

Procedure DrawPixel(selectedColor)
  mx=GetMouseX(2) : my=GetMouseY(2) 
  mxx=(mx/#CELLSIZE) * #CELLSIZE : myy=(my/#CELLSIZE) * #CELLSIZE 
  StartDrawing(ImageOutput(0))  
  RoundBox(mxx,myy, #CELLSIZE - 3 ,#CELLSIZE - 3,2,2, selectedColor) 
  StopDrawing() 
  SetGadgetState(2, ImageID(0))
EndProcedure   

Repeat 
  Select WaitWindowEvent(): 
    Case #PB_Event_CloseWindow: End 
      
    Case #WM_LBUTTONDOWN
      DrawPixel(selectedColor)
      
    Case #WM_RBUTTONDOWN
      delmode=1
      DrawPixel(0)
      
    Case #WM_RBUTTONUP
      delmode=0
      
    Case #WM_MOUSEMOVE 
      If (GetAsyncKeyState_(#VK_LBUTTON)  & $10000)  Or (GetAsyncKeyState_(#VK_RBUTTON)  & $10000)  ;; (mouse button is down)
        mx=GetMouseX(2) : my=GetMouseY(2) 
        mxx=(mx/#CELLSIZE) * #CELLSIZE : myy=(my/#CELLSIZE) * #CELLSIZE 
        
        StartDrawing(ImageOutput(0)) 
        If delmode
          RoundBox(mxx,myy, #CELLSIZE - 3, #CELLSIZE - 3,2,2, 0) 
        Else          
          RoundBox(mxx,myy, #CELLSIZE - 3, #CELLSIZE - 3,2,2, selectedColor) 
        EndIf 
        
        StopDrawing() 
        SetGadgetState(2, ImageID(0))
      EndIf
      
    Case #PB_Event_Gadget 
      Select EventGadget() 
        Case 3
          If EventType() = #PB_EventType_LeftClick
            StartDrawing(CanvasOutput(3))
            selectedColor = Point(GetMouseX(3), GetMouseY(3))
            Box(1, 1, 3, WindowHeight(0), selectedColor)
            StopDrawing()
          EndIf 
        Case 5 
          End 
      EndSelect 
  EndSelect 
ForEver 
Last edited by firace on Tue Nov 16, 2021 9:48 pm, edited 1 time in total.
Mr.L
Enthusiast
Enthusiast
Posts: 104
Joined: Sun Oct 09, 2011 7:39 am

Re: PixelToy

Post by Mr.L »

Plain and simple! Very nice :D
Image
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: PixelToy

Post by firace »

Thanks! Very nice drawing!

I've just updated the code with some little bugfixes.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: PixelToy

Post by Cyllceaux »

Image
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: PixelToy

Post by AZJIO »

Transparency?
Saving in ico?
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: PixelToy

Post by AZJIO »

firace
I used CanvasGadget to make the editor cross-platform. I removed messages #WM_LBUTTONDOWN, #WM_RBUTTONDOWN, #WM_LBUTTONUP, #WM_RBUTTONUP, #WM_MOUSEMOVE and functions GetCursorPos_(), MapWindowPoints_()

I also used the variables mxxOld, myyOld so as not to redraw the square until the cursor moves to another square.

Code: Select all

Case #cnvScene
	ecnv = EventType()
	Select ecnv
		Case #PB_EventType_RightButtonDown
			delmode = 1
			DrawPixel(0)
			pendown = 1
		Case #PB_EventType_LeftButtonDown
			DrawPixel(selectedColor)
			pendown = 1
		Case #PB_EventType_LeftButtonUp, #PB_EventType_RightButtonUp
			pendown = 0
			delmode = 0
		Case #PB_EventType_MouseMove
			If pendown = 1
				; преобразование в позицию квадратов x, y, чтобы сравнить являются ли квадраты на одной линии
				mxx = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseX) / #CELLSIZEW) * #CELLSIZEW
				myy = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseY) / #CELLSIZEH) * #CELLSIZEH
				If mxxOld = mxx And myyOld = myy
					Continue
				Else
					mxxOld = mxx
					myyOld = myy
				EndIf
				
				If StartDrawing(CanvasOutput(#cnvScene))
					If delmode
						Select StyleBox
							Case 0
								Box(mxx, myy, #CELLSIZEW, #CELLSIZEH, 0)
							Case 1
								Box(mxx, myy, #CELLSIZEW - 1, #CELLSIZEH - 1, 0)
							Case 2
								RoundBox(mxx, myy, #CELLSIZEW - 3, #CELLSIZEH - 3, 2, 2, 0)
						EndSelect
					Else
						Select StyleBox
							Case 0
								Box(mxx, myy, #CELLSIZEW, #CELLSIZEH, selectedColor)
							Case 1
								Box(mxx, myy, #CELLSIZEW - 1, #CELLSIZEH - 1, selectedColor)
							Case 2
								RoundBox(mxx, myy, #CELLSIZEW - 3, #CELLSIZEH - 3, 2, 2, selectedColor)
						EndSelect
					EndIf
					StopDrawing()
				EndIf
			EndIf
	EndSelect
EndSelect
You can get the full source here.
Thanks for the idea.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: PixelToy

Post by Mijikai »

Nice!
Image
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

Re: PixelToy

Post by AZJIO »

You can open the icon and save it to a bmp file
Image

Code: Select all

; AZJIO (19.11.2023)

EnableExplicit

#Window = 0

;- Enumeration
Enumeration Gadget
	#cnvPalette
	#cnvScene
; 	#btnOpenLevel
	#btnSave
	#btnOpenImg
	#btnClear
	#Opt1
	#Opt2
	#Opt3
EndEnumeration

Declare hsb_to_rgb()
Declare rgb_to_hsb()

UseGIFImageDecoder()
UsePNGImageDecoder()

;- Global
Global tmp, i, j, y, x, Color, Type, ecnv, selectedColor, StyleBox
Global mxxOld, myyOld, pendown, delmode, mxx, myy
Global tmp$
Global ImagePlugin
Global Dim arr_rgb(2)
Global Dim arr_hsb(2)
Global w, h

Global ini$ = GetPathPart(ProgramFilename()) + "BallBrick.ini"





Global CELLSIZEW = 20
Global CELLSIZEH = 20
; #CELLSIZE
Global XXX = 24
Global YYY = 24

; Global Dim aIcon(XXX, YYY)
Global Dim aIcon(XXX, YYY)
Global AreaX = CELLSIZEW * XXX
Global AreaY = CELLSIZEH * YYY

Structure Bricks
	x.i
	y.i
	t.i
	c.i
EndStructure

;- GUI
OpenWindow(#Window, 0, 0, AreaX + 110, AreaY + 4, "IconEditor", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
; SetWindowColor(#Window, 0)

OptionGadget(#Opt1, AreaX + 11, AreaY - 230, 98, 20, "Solid")
OptionGadget(#Opt2, AreaX + 11, AreaY - 210, 98, 20, "Box")
OptionGadget(#Opt3, AreaX + 11, AreaY - 190, 98, 20, "Round")
SetGadgetState(#Opt2, 1)
StyleBox = 1

; ButtonGadget(#btnOpenLevel, AreaX + 6, AreaY - 160, 100, 33 , "Level")
ButtonGadget(#btnClear, AreaX + 6, AreaY - 120, 100, 33 , "Clear")
ButtonGadget(#btnOpenImg, AreaX + 6, AreaY - 80, 100, 33 , "Open img")
ButtonGadget(#btnSave, AreaX + 6, AreaY - 40, 100, 33 , "Save")

; холст Сцена
CanvasGadget(#cnvScene, 0, 0, AreaX + 4, AreaY + 4, #PB_Canvas_Border)
If StartDrawing(CanvasOutput(#cnvScene))
	Box(0, 0, AreaX, AreaY, 0)
	StopDrawing()
EndIf


; холст Палитра
CanvasGadget(#cnvPalette, AreaX + 4, 0, 106 , AreaY - 248)

If StartDrawing(CanvasOutput(#cnvPalette))

	Box(0, 0, 110 , AreaY - 168 , $333333)



	; Спектр
	w = 100
	h = 10
	arr_hsb(1) = 100
	arr_hsb(2) = 100

	For i = 100 To 0 Step - 1
		Line(93, 150 - i, 10, 1, RGB(i * 2.5, i * 2.5, i * 2.5))
	Next
; 	For i=0 To 255
; 		Line(91, i, 10, 1, RGB(i, i, i))
; 	Next

	For j = 0 To 90
		arr_hsb(2) = 100 - j
		For i = 0 To w
			arr_hsb(0) = i * 360 / w
			hsb_to_rgb()
			Plot(j + 1, i + 50, RGB(arr_rgb(0), arr_rgb(1), arr_rgb(2)))
		Next
	Next


	selectedColor = $7DC4DF
; 	Box(0, 0, 110, 30, selectedColor)
	Box(27, 8, 32, 32, $FFFFFF)
	Box(28, 9, 30, 30, selectedColor)

	StopDrawing()

EndIf

;; init palette end

Procedure DrawPixel(selectedColor)
	Protected myy, mxx
	; преобразование в позицию квадратов x, y, чтобы сравнить являются ли квадраты на одной линии
	mxx = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseX) / CELLSIZEW) * CELLSIZEW
	myy = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseY) / CELLSIZEH) * CELLSIZEH
	If StartDrawing(CanvasOutput(#cnvScene))
		Select StyleBox
			Case 0
				Box(mxx, myy, CELLSIZEW, CELLSIZEH, selectedColor)
			Case 1
				Box(mxx, myy, CELLSIZEW - 1, CELLSIZEH - 1, selectedColor)
			Case 2
				RoundBox(mxx + 1, myy + 1, CELLSIZEW - 2, CELLSIZEH - 2, 2, 2, selectedColor)
		EndSelect
		StopDrawing()
	EndIf
EndProcedure



;- Loop
Repeat
	Select WaitWindowEvent():
		Case #PB_Event_CloseWindow
			CloseWindow(#Window)
			End

			;  $00FF99
;- Loop_Gadget
		Case #PB_Event_Gadget
			Select EventGadget()
				Case #Opt1
					; 					SetGadgetState(#Opt1, 1)
					StyleBox = 0
				Case #Opt2
; 					SetGadgetState(#Opt2, 1)
					StyleBox = 1
				Case #Opt3
; 					SetGadgetState(#Opt3, 1)
					StyleBox = 2
; 				Case #btnOpenLevel
; 					tmp$ = OpenFileRequester("Открыть уровень", GetCurrentDirectory(), "txt|*.txt", 0)
; 					If Asc(tmp$)
; 					EndIf
				Case #btnClear
					If StartDrawing(CanvasOutput(#cnvScene))
						Box(0, 0, AreaX, AreaY, 0)
						StopDrawing()
					EndIf
				Case #btnOpenImg
					tmp$ = OpenFileRequester("Open file", GetCurrentDirectory(), "img|*.bmp;*.png;*.gif;*.ico", 0)
					If Asc(tmp$)
						#Image = 0
						If LoadImage(#Image, tmp$)
							If StartDrawing(CanvasOutput(#cnvScene))
								If GetExtensionPart(tmp$) = "ico"
									XXX = ImageWidth(#Image)
									YYY = ImageHeight(#Image)
									; Debug XXX
									; Debug YYY
									Box(0, 0, XXX + 1, YYY + 1, $010203) ; стираем холст цветом, который задан как прозрачный
									Dim aIcon(XXX, YYY) ; пересоздаём массив, в отличии от ReDim Dim стирает данные, но они и не нужны.
									CELLSIZEW = AreaX / XXX
									CELLSIZEH = AreaY / YYY
								Else
									ResizeImage(#Image, XXX , YYY, #PB_Image_Smooth)
; 									ResizeImage(#Image, AreaX , AreaY, #PB_Image_Raw)
								EndIf
								SetWindowTitle(#Window, "IconEditor   (" + Str(XXX) + "x" + Str(YYY) + ")")
								DrawImage(ImageID(#Image), 0, 0)
								; 	считываем пиксели рисунка в массив
								For y = 0 To YYY - 1
									For x = 0 To XXX - 1
										tmp = Point(x, y)
; 										Debug tmp
										aIcon(x, y) = tmp
									Next
								Next
								Box(0, 0, AreaX, AreaY, $010203) ; стираем холст
								For y = 0 To YYY - 1
									For x = 0 To XXX - 1
										Select StyleBox
											Case 0
												Box(x * CELLSIZEW, y * CELLSIZEH, CELLSIZEW, CELLSIZEH, aIcon(x, y))
											Case 1
												Box(x * CELLSIZEW, y * CELLSIZEH, CELLSIZEW - 1, CELLSIZEH - 1, aIcon(x, y))
											Case 2
												RoundBox(x * CELLSIZEW + 1, y * CELLSIZEH + 1, CELLSIZEW - 2, CELLSIZEH - 2, 2, 2, aIcon(x, y))
										EndSelect
									Next
								Next
								StopDrawing()
							EndIf
							FreeImage(#Image)
						EndIf
					EndIf
				Case #btnSave
					If StartDrawing(CanvasOutput(#cnvScene))
						DrawingMode(#PB_2DDrawing_Transparent)
						For y = 1 To YYY
							For x = 1 To XXX
								tmp = Point(x * CELLSIZEW - CELLSIZEW / 2, y * CELLSIZEH - CELLSIZEH / 2)
								aIcon(x, y) = tmp
							Next
						Next
						StopDrawing()
						If CreateImage(#Image, XXX, YYY, 24, #PB_Image_Transparent)
; 						If CreateImage(#Image, XXX, YYY)
							If StartDrawing(ImageOutput(#Image))
								DrawingMode(#PB_2DDrawing_Transparent)
								For y = 1 To YYY
									For x = 1 To XXX
										If aIcon(x, y) <> $010203
											Plot(x - 1, y - 1, aIcon(x, y))
										EndIf
									Next
								Next
								StopDrawing()
								; tmp$ = SaveFileRequester("Выберите файл для сохранения", GetCurrentDirectory(), "img|*.bmp;*.png;*.gif;*.ico", 0)
								tmp$ = SaveFileRequester("Select file to save", GetCurrentDirectory(), "*.bmp|*.bmp|*.png|*.png", 0)
								If Asc(tmp$)
									Select GetExtensionPart(tmp$)
										Case "png"
											ImagePlugin = #PB_ImagePlugin_PNG
										Case "bmp"
											ImagePlugin = #PB_ImagePlugin_BMP
										Default
											tmp$ + ".bmp"
											ImagePlugin = #PB_ImagePlugin_BMP
									EndSelect
									; 									Debug tmp$
; 									выводим на холст палитры, всё нормально с изображением
; 									If StartDrawing(CanvasOutput(#cnvPalette))
; 										DrawImage(ImageID(#Image), 1, 1)
; 										StopDrawing()
; 									EndIf
									If SaveImage(#Image, tmp$, ImagePlugin)
										MessageRequester("File created", tmp$)
									Else
										MessageRequester("Error", "Failed to save file")
									EndIf
								EndIf
							EndIf
							FreeImage(#Image)
						EndIf
					EndIf


;- Canvas (event)
				Case #cnvPalette
					ecnv = EventType()
					Select ecnv
						Case #PB_EventType_MouseWheel
; 							Debug
							mxx = GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseX)
							myy = GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseY)
							If mxx > 0 And mxx < 92 And myy > 49 And myy < 151
								arr_hsb(1) + GetGadgetAttribute(#cnvPalette, #PB_Canvas_WheelDelta) * 5
								If arr_hsb(1) < 0
									arr_hsb(1) = 0
									Continue
								ElseIf arr_hsb(1) > 100
									arr_hsb(1) = 100
									Continue
								EndIf
								Debug arr_hsb(1)
								If StartDrawing(CanvasOutput(#cnvPalette))
									For j = 0 To 90
										arr_hsb(2) = 100 - j
										For i = 0 To w
											arr_hsb(0) = i * 360 / w
											hsb_to_rgb()
											Plot(j + 1, i + 50, RGB(arr_rgb(0), arr_rgb(1), arr_rgb(2)))
										Next
									Next
									StopDrawing()
								EndIf
							EndIf
						Case #PB_EventType_LeftClick
							mxx = GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseX)
							myy = GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseY)
; 							Debug mxx
; 							Debug myy
							If mxx > 0 And mxx < 92 And myy > 49 And myy < 151
								If StartDrawing(CanvasOutput(#cnvPalette))
									selectedColor = Point(GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseX), GetGadgetAttribute(#cnvPalette, #PB_Canvas_MouseY))

									Box(28, 9, 30, 30, selectedColor)
									StopDrawing()
								EndIf
							EndIf
							If mxx > 92 And mxx < 104 And myy > 49 And myy < 151
								arr_hsb(1) = 150 - myy
								; 								Debug arr_hsb(1)
								If StartDrawing(CanvasOutput(#cnvPalette))
									For j = 0 To 90
										arr_hsb(2) = 100 - j
										For i = 0 To w
											arr_hsb(0) = i * 360 / w
											hsb_to_rgb()
											Plot(j + 1, i + 50, RGB(arr_rgb(0), arr_rgb(1), arr_rgb(2)))
										Next
									Next
									StopDrawing()
								EndIf
							EndIf
					EndSelect
				Case #cnvScene
					ecnv = EventType()
					Select ecnv
						Case #PB_EventType_RightButtonDown
							delmode = 1
							DrawPixel(0)
							pendown = 1
						Case #PB_EventType_LeftButtonDown
							DrawPixel(selectedColor)
							pendown = 1
						Case #PB_EventType_LeftButtonUp, #PB_EventType_RightButtonUp
							pendown = 0
							delmode = 0
						Case #PB_EventType_MouseMove
							If pendown = 1
								; преобразование в позицию квадратов x, y, чтобы сравнить являются ли квадраты на одной линии
								mxx = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseX) / CELLSIZEW) * CELLSIZEW
								myy = Int(GetGadgetAttribute(#cnvScene, #PB_Canvas_MouseY) / CELLSIZEH) * CELLSIZEH
								If mxxOld = mxx And myyOld = myy
									Continue
								Else
									mxxOld = mxx
									myyOld = myy
								EndIf

								If StartDrawing(CanvasOutput(#cnvScene))
									If delmode
										Select StyleBox
											Case 0
												Box(mxx, myy, CELLSIZEW, CELLSIZEH, 0)
											Case 1
												Box(mxx, myy, CELLSIZEW - 1, CELLSIZEH - 1, 0)
											Case 2
												RoundBox(mxx + 1, myy + 1, CELLSIZEW - 2, CELLSIZEH - 2, 2, 2, 0)
										EndSelect
									Else
										Select StyleBox
											Case 0
												Box(mxx, myy, CELLSIZEW, CELLSIZEH, selectedColor)
											Case 1
												Box(mxx, myy, CELLSIZEW - 1, CELLSIZEH - 1, selectedColor)
											Case 2
												RoundBox(mxx + 1, myy + 1, CELLSIZEW - 2, CELLSIZEH - 2, 2, 2, selectedColor)
										EndSelect
									EndIf
									StopDrawing()
								EndIf
							EndIf
					EndSelect
			EndSelect
	EndSelect
ForEver



; Procedure hsb_to_rgb(arr_hsb)
Procedure hsb_to_rgb()
	Protected sector
	Protected.f ff, pp, qq, tt
	Protected.f Dim af_rgb(2) ; создаём массивы в которых числа будут в диапазоне 0-1
	Protected.f Dim af_hsb(2)
	; Protected Dim arr_rgb(2)

	af_hsb(2) = arr_hsb(2) / 100

	If arr_hsb(1) = 0 ; если серый, то одно значение всем
		arr_rgb(0) = Round(af_hsb(2) * 255, #PB_Round_Nearest)
		arr_rgb(1) = arr_rgb(0)
		arr_rgb(2) = arr_rgb(0)
		; ProcedureReturn arr_rgb
	EndIf

	While arr_hsb(0) >= 360 ; если тон задан большим запредельным числом, то
		arr_hsb(0) - 360
	Wend

	af_hsb(1) = arr_hsb(1) / 100
	af_hsb(0) = arr_hsb(0) / 60
	; sector = Int(arr_hsb(0))
	sector = Round(af_hsb(0), #PB_Round_Down)

	ff = af_hsb(0) - sector
	pp = af_hsb(2) * (1 - af_hsb(1))
	qq = af_hsb(2) * (1 - af_hsb(1) * ff)
	tt = af_hsb(2) * (1 - af_hsb(1) * (1 - ff))

	Select sector
		Case 0
			af_rgb(0) = af_hsb(2)
			af_rgb(1) = tt
			af_rgb(2) = pp
		Case 1
			af_rgb(0) = qq
			af_rgb(1) = af_hsb(2)
			af_rgb(2) = pp
		Case 2
			af_rgb(0) = pp
			af_rgb(1) = af_hsb(2)
			af_rgb(2) = tt
		Case 3
			af_rgb(0) = pp
			af_rgb(1) = qq
			af_rgb(2) = af_hsb(2)
		Case 4
			af_rgb(0) = tt
			af_rgb(1) = pp
			af_rgb(2) = af_hsb(2)
		Default
			af_rgb(0) = af_hsb(2)
			af_rgb(1) = pp
			af_rgb(2) = qq
	EndSelect

	; RGB
	arr_rgb(0) = Round(af_rgb(0) * 255, #PB_Round_Nearest)
	arr_rgb(1) = Round(af_rgb(1) * 255, #PB_Round_Nearest)
	arr_rgb(2) = Round(af_rgb(2) * 255, #PB_Round_Nearest)

	; BGR
	; arr_rgb(2)=Round(af_rgb(0)*255, #PB_Round_Nearest)
	; arr_rgb(1)=Round(af_rgb(1)*255, #PB_Round_Nearest)
	; arr_rgb(0)=Round(af_rgb(2)*255, #PB_Round_Nearest)

	; ProcedureReturn arr_rgb
EndProcedure


Procedure rgb_to_hsb()
	Protected.f min, max

	If arr_rgb(0) <= arr_rgb(1)
		min = arr_rgb(0)
		max = arr_rgb(1)
	Else
		min = arr_rgb(1)
		max = arr_rgb(0)
	EndIf

	If min > arr_rgb(2)
		min = arr_rgb(2)
	EndIf

	If max < arr_rgb(2)
		max = arr_rgb(2)
	EndIf

	If max = min
		arr_hsb(0) = 0
	ElseIf max = arr_rgb(0)
		arr_hsb(0) = 60 * (arr_rgb(1) - arr_rgb(2)) / (max - min)
		If arr_rgb(1) < arr_rgb(2)
			arr_hsb(0) + 360
		EndIf
	ElseIf max = arr_rgb(1)
		arr_hsb(0) = 60 * (arr_rgb(2) - arr_rgb(0)) / (max - min) + 120
	ElseIf max = arr_rgb(2)
		arr_hsb(0) = 60 * (arr_rgb(0) - arr_rgb(1)) / (max - min) + 240
	EndIf

	If max = 0
		arr_hsb(1) = 0
	Else
		arr_hsb(1) = (1 - min / max) * 100
	EndIf

	arr_hsb(2) = max / 255 * 100

	arr_hsb(0) = Round(arr_hsb(0), #PB_Round_Nearest)
	arr_hsb(1) = Round(arr_hsb(1), #PB_Round_Nearest)
	arr_hsb(2) = Round(arr_hsb(2), #PB_Round_Nearest)

	; ProcedureReturn arr_hsb
EndProcedure
Last edited by AZJIO on Sun Nov 19, 2023 10:38 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PixelToy

Post by idle »

Nice one AZJIO :D
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: PixelToy

Post by firace »

Great improvements! Thanks AZJIO.
User avatar
aston
User
User
Posts: 64
Joined: Wed Nov 18, 2009 11:18 pm

Re: PixelToy

Post by aston »

hi
i don't have CanvasGadget in my old PB4.5
so ..can i replace it with something else ?
tnx
Post Reply