Color Convert Functions


/ Published in: Maxscript
Save to your folder(s)

Easy way to convert color values using MXS


Copy this code and paste it in your HTML
  1. --CONVERT RGB (0-255) TO HEXSTRING
  2. fn RGBtoHEX clrRGB = (bit.intAsHex clrRGB.r)+(bit.intAsHex clrRGB.g)+(bit.intAsHex clrRGB.b)
  3. RGBtoHEX (color 80 170 178)
  4. -->"50aab2"
  5. --CONVERT HEXSTRING TO RGB (0-255)
  6. fn HEXtoRGB hexString =
  7. (
  8. local clr, hexValue = true, hexChars = "AaBbCcDdEeFe0123456789"
  9. if hexString[1] == "#" do trimleft hexString "#"
  10. if ((for c in 1 to str.count where findstring hexChars str[c] == undefined collect str[c]).count != 0) do hexValue = false
  11. if (hexValue == true and hexString.count == 6) do
  12. (
  13. local clrArr = for i = 1 to 5 where mod i 2 != 0 collect (append hexString[i] hexString[i+1])
  14. clr = (color (bit.hexAsInt clrArr[1]) (bit.hexAsInt clrArr[2]) (bit.hexAsInt clrArr[3]))
  15. ) ; return clr
  16. )
  17. HEXtoRGB "50aab2"
  18. -->(color 80 170 178)
  19. --CONVERT RGB (0-255) TO FLOAT POINT COLOR
  20. fn RGBtoFPC clrRGB = (color (clrRGB.r/255) (clrRGB.g/255) (clrRGB.b/255))
  21. RGBtoFPC (color 80 170 178)
  22. -->(color 0.313726 0.666667 0.698039)
  23. --CONVERT FLOAT POINT COLOR TO RGB (0-255)
  24. fn FPCtoRGB clrFPC = (color (clrFPC.r*255) (clrFPC.g*255) (clrFPC.b*255))
  25. FPCtoRGB (color 0.313726 0.666667 0.698039)
  26. -->(color 80 170 178)
  27. --CONVERT DOTNET COLOR TO RGB (0-255)
  28. fn DOTNETtoMXSRGB dnClr = (local clr ; clr = if classof dnClr.r == Integer do (color dnClr.r dnClr.g dnClr.b) ; clr)
  29. DOTNETtoMXSRGB ((dotNetClass "System.Drawing.Color").LightGray)
  30. -->(color 211 211 211)
  31. --CONVERT DOTNET COLOR TO FLOAT POINT COLOR
  32. fn DOTNETtoMXSFPC dnClr = (local clr ; clr = if classof dnClr.r == Integer do (color ((dnClr.r as Float)/255) ((dnClr.g as Float)/255) ((dnClr.b as Float)/255)) ; clr)
  33. DOTNETtoMXSFPC ((dotNetClass "System.Drawing.Color").LightGray)
  34. -->(color 0.827451 0.827451 0.827451)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.