Well I guess I'll play around a bit. I don't have DBC yet though. Here is a version in DBP.
`convert rgb to hex
`convert rgb to hex string
a$ = rgb_to_hex(128,128,128)
`convert hex to r
r = hex_to_r(a$)
`convert hex to g
g = hex_to_g(a$)
`convert hex to b
b = hex_to_b(a$)
`print results
print "Hex = ",a$
print "r = ",r
print "g = ",g
print "b = ",b
wait key
end
function rgb_to_hex(a,b,c)
`convert r value to hex string
temp$ = hex$(a)
`HexR is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`save conveted r
d$ = temp$
`convert g value to hex string
temp$ = hex$(b)
`HexB is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`add converted g to converted r
d$ = d$+temp$
`convert b value to hex string
temp$ = hex$(c)
`HexG is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`put leading # in front and
`add converted b to r and g
d$ = "#"+d$+temp$
endfunction d$
function hex_to_r(a$)
`convert hex to r value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of r_hex
`use first character in the hex string
temp1 = hex_to_dec(mid$(a$,1,1))
`since r_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of r_hex
`it is the lower bits so no conversion needed
`use second character in the hex string
temp2 = hex_to_dec(mid$(a$,2,1))
`add the 2 conversions for r value
rval = temp1 + temp2
endfunction rval
function hex_to_g(a$)
`convert hex to g value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of g_hex
`use third character in the hex string
temp1 = hex_to_dec(mid$(a$,3,1))
`since g_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of g_hex
`it is the lower bits so no conversion needed
`use forth character in the hex string
temp2 = hex_to_dec(mid$(a$,4,1))
`add the 2 conversions for g value
gval = temp1 + temp2
endfunction gval
function hex_to_b(a$)
`convert hex to b value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of b_hex
`use fifth character in the hex string
temp1 = hex_to_dec(mid$(a$,5,1))
`since b_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of b_hex
`it is the lower bits so no conversion needed
`use sixth character in the hex string
temp2 = hex_to_dec(mid$(a$,6,1))
`add the 2 conversions for b value
bval = temp1 + temp2
endfunction bval
function hex_to_dec(a$)
`convert 4bit hex to dec
`save calced value
value = val(a$)
`if less than or equal to 0
`(has letters in it) select value by letter
`else keep value
if value <= 0
select a$
case "A"
decval = 10
endcase
case "B"
decval = 11
endcase
case "C"
decval = 12
endcase
case "D"
decval = 13
endcase
case "E"
decval = 14
endcase
case "F"
decval = 15
endcase
case default
decval = 0
endcase
endselect
else
decval = value
endif
endfunction decval
And here it is using the mouse to pick the color.
`convert rgb to hex
sync on : sync rate 0
backdrop on : color backdrop 0
`make image
create bitmap 1,256,128
for i = 0 to 256
for j = 0 to 128
dot i,j,rgb(rnd(255),rnd(255),rnd(255))
next j
next i
get image 1,1,1,256,128
cls
delete bitmap 1
`set variable to store screen rgb
sample as dword
do
`paste image for testing
paste image 1,150,150
`capture screen rgb
sample = point(mousex(),mousey())
`convert rgb to hex string
a$ = rgb_to_hex(rgbr(sample),rgbg(sample),rgbb(sample))
`convert hex to r
r = hex_to_r(a$)
`convert hex to g
g = hex_to_g(a$)
`convert hex to b
b = hex_to_b(a$)
`print results
set cursor 0,5
print "Hex = ",a$
print "r = ",r
print "g = ",g
print "b = ",b
print "Frames Per Second :",screen fps()
sync
loop
end
function rgb_to_hex(a,b,c)
`convert r value to hex string
temp$ = hex$(a)
`HexR is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`save conveted r
d$ = temp$
`convert g value to hex string
temp$ = hex$(b)
`HexB is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`add converted g to converted r
d$ = d$+temp$
`convert b value to hex string
temp$ = hex$(c)
`HexG is 8bit word so it needs 2 strings (2 @ 4bits)
`if only one string is converted add leading 0
if len(temp$) = 1
temp$ = "0"+temp$
endif
`put leading # in front and
`add converted b to r and g
d$ = "#"+d$+temp$
endfunction d$
function hex_to_r(a$)
`convert hex to r value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of r_hex
`use first character in the hex string
temp1 = hex_to_dec(mid$(a$,1,1))
`since r_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of r_hex
`it is the lower bits so no conversion needed
`use second character in the hex string
temp2 = hex_to_dec(mid$(a$,2,1))
`add the 2 conversions for r value
rval = temp1 + temp2
endfunction rval
function hex_to_g(a$)
`convert hex to g value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of g_hex
`use third character in the hex string
temp1 = hex_to_dec(mid$(a$,3,1))
`since g_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of g_hex
`it is the lower bits so no conversion needed
`use forth character in the hex string
temp2 = hex_to_dec(mid$(a$,4,1))
`add the 2 conversions for g value
gval = temp1 + temp2
endfunction gval
function hex_to_b(a$)
`convert hex to b value
`subtract the leading # from hex
a$ = right$(a$, len(a$)-1)
`convert first character of b_hex
`use fifth character in the hex string
temp1 = hex_to_dec(mid$(a$,5,1))
`since b_hex is 8bit convert first character to upper bits
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
temp1 = temp1*2
`convert second character of b_hex
`it is the lower bits so no conversion needed
`use sixth character in the hex string
temp2 = hex_to_dec(mid$(a$,6,1))
`add the 2 conversions for b value
bval = temp1 + temp2
endfunction bval
function hex_to_dec(a$)
`convert 4bit hex to dec
`save calced value
value = val(a$)
`if less than or equal to 0
`(has letters in it) select value by letter
`else keep value
if value <= 0
select a$
case "A"
decval = 10
endcase
case "B"
decval = 11
endcase
case "C"
decval = 12
endcase
case "D"
decval = 13
endcase
case "E"
decval = 14
endcase
case "F"
decval = 15
endcase
case default
decval = 0
endcase
endselect
else
decval = value
endif
endfunction decval
