@Beckett2000 I don’t know if you’re using WebRepo yet, but I have an RGB to HSV program there. If you don’t, here is the program.
-- Convert RGB to HSV
viewer.mode=FULLSCREEN
function setup()
rectMode(CENTER)
boxR=input(WIDTH/2-100,HEIGHT-200,90,30,1,"R")
boxG=input(WIDTH/2-100,HEIGHT-275,90,30,1,"G")
boxB=input(WIDTH/2-100,HEIGHT-350,90,30,1,"B")
boxH=input(WIDTH/2,HEIGHT-200,90,30,2,"H")
boxS=input(WIDTH/2,HEIGHT-275,90,30,2,"S")
boxV=input(WIDTH/2,HEIGHT-350,90,30,2,"V")
boxC=input(WIDTH/2+100,HEIGHT-200,90,30,2,"RGB Color")
boxX=input(WIDTH/2+100,HEIGHT-275,90,30,2,"RGB Hex")
boxTab={boxR,boxG,boxB,boxH,boxS,boxV,boxC,boxX}
end
function draw()
background(32, 59, 83) -- screen color
headings()
for nbr,box in pairs(boxTab) do -- loop thru table
box:draw() -- draw each box
end
end
function headings()
pushStyle()
fontSize(36)
fill(255,0,0)
text("RGB to HSV Converter",WIDTH/2,HEIGHT-50) -- draw text
fontSize(20)
fill(224, 149, 127, 255) -- set color
text("( Tap each R, G, B box to input a value )",WIDTH/2,HEIGHT-100)
popStyle()
end
function touched(t) -- check which box is selected
for nbr,box in pairs(boxTab) do -- loop thru table
if box:touched(t) then -- box touched
return -- exit function
end
end
end
function keyboard(k)
for nbr,box in pairs(boxTab) do -- loop thru table
box:keyboard(k) -- get input from box
end
end
input=class()
input.msg=""
input.str=""
function input:init(x,y,w,h,type,txt)
self.x=x -- x position
self.y=y -- y position
self.w=w -- width
self.h=h -- height
self.type=type -- 1=input box 2=output box
self.txt=txt -- text to show above box
self.val="0" -- data keyed in box
self.sel=false -- box selected true/false
end
function input:draw()
pushStyle()
fill(0) -- set background for box
if self.txt=="RGB Color" then
self.val=""
fill(boxR.val,boxG.val,boxB.val)
end
if self.txt=="RGB Hex" then
self.val=string.format("%02X%02X%02X",boxR.val,boxG.val,boxB.val)
end
if self.sel then -- set selected color for box
fill(105, 101, 31, 255)
if input.msg~="" then
fill(0,255,0)
text(input.msg,WIDTH/2,HEIGHT-140)
end
end
stroke(255) -- box outline color
strokeWidth(2) -- outline size
rect(self.x,self.y,self.w,self.h) -- draw box
fill(255) -- set text color
text(self.txt,self.x,self.y+self.h) -- box name
text(self.val,self.x+4,self.y) -- box text
popStyle()
end
function input:keyboard(k)
if self.sel then
if input.msg~="" then
input.msg=""
self.val="0"
input.str="0"
end
if k==BACKSPACE then -- backspace key pressed
input.str=string.sub(input.str,1,#input.str-1) -- remove last digit
if #input.str==0 then
input.str="0"
end
elseif k>="0" and k<="9" then -- only digits 1 thru 9
if string.sub(input.str,1,1) =="0" then
input.str="" -- clear leading 0
end
input.str=input.str..k -- update keyed value
end
self.val=input.str -- save keyed value
if math.tointeger(self.val)>255 then -- check valid range
input.str=""
input.msg="ERROR: Must be 0-255"
end
input:convert()
end
end
function input:touched(t)
if t.state==BEGAN then
input.msg=""
if not isKeyboardShowing() then
showKeyboard() -- show keyboard if its not displayed
end
input.str="0" -- set to 0
for z=1,#boxTab do -- clear selected flag for all boxes
boxTab[z].sel=false
end
-- check which box was selected
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 and self.type==1 then
self.val="0" -- reset val
self.sel=true -- set selected flag
return true -- box selected
end
end
return false -- no box selected
end
function input:convert() -- convert RGB to HSV
local r=math.tointeger(boxR.val)/255
local g=math.tointeger(boxG.val)/255
local b=math.tointeger(boxB.val)/255
local max,min=math.max(r,g,b),math.min(r,g,b)
local diff=max-min h,s,v=0,0,0
if diff==0 then
h=0
else
if max==r then
h=(((g-b)/diff)%6)*60
end
if max==g then
h=(((b-r)/diff)+2)*60
end
if max==b then
h=(((r-g)/diff)+4)*60
end
end
if max==0 then
s=0
else
s=(diff/max)
end
boxH.val=string.format("%d°",h//1)
boxS.val=string.format("%2.1f%%",s*100)
boxV.val=string.format("%2.1f%%",max*100)
end