Skip to content

Instantly share code, notes, and snippets.

@terrelcodes
Created January 31, 2023 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrelcodes/2717f5661e24c19794dbe03fa276a0d3 to your computer and use it in GitHub Desktop.
Save terrelcodes/2717f5661e24c19794dbe03fa276a0d3 to your computer and use it in GitHub Desktop.
First attempt at contributing to Pyret
fun code-point-to-hex-digit( d :: Number ) -> Number:
doc: "convert a code point (letter) to a hex digit"
ask:
| (d >= 48) and (d <= 57) then: d - 48
| (d >= 97) and (d <= 103) then: d - 87
| (d >= 65) and (d <= 71) then: d - 55
end
where:
code-point-to-hex-digit(string-to-code-point('0')) is 0
code-point-to-hex-digit(string-to-code-point('2')) is 2
code-point-to-hex-digit(string-to-code-point('9')) is 9
code-point-to-hex-digit(string-to-code-point('A')) is 10
code-point-to-hex-digit(string-to-code-point('a')) is 10
code-point-to-hex-digit(string-to-code-point('F')) is 15
code-point-to-hex-digit(string-to-code-point('f')) is 15
end
fun hex-string-to-number( digits :: String ) -> Number:
doc: "convert a String of hex digits to a Number"
for fold(sum from 0, digit from string-to-code-points(digits)):
(sum * 16) + code-point-to-hex-digit(digit)
end
where:
hex-string-to-number('0') is 0
hex-string-to-number('ff') is 255
hex-string-to-number('ffff') is 65535
end
fun hexcolor( rgb :: String )-> Color:
doc: "convert an RGB hex triple into an opaque color"
r = hex-string-to-number(string-substring(rgb,0,2))
g = hex-string-to-number(string-substring(rgb,2,4))
b = hex-string-to-number(string-substring(rgb,4,6))
color(r,g,b,1)
where:
hexcolor( "FF7f00" ) is color(255,127,0,1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment