ColorUtil_Tips.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. ---@class ColorUtil @注释
  2. ColorUtil = class()
  3. local this = ColorUtil
  4. ---@param htmlString string @ #ffffff
  5. function this.TryParseHtmlStringToVector4(htmlString)
  6. ---@type Color
  7. local b, outColor = ColorUtility.TryParseHtmlString(htmlString)
  8. if b then
  9. return Vector4(outColor.r, outColor.g, outColor.b, outColor.a)
  10. end
  11. return nil
  12. end
  13. function this.TryParseHtmlStringToColor(htmlString)
  14. ---@type Color
  15. local b, outColor = ColorUtility.TryParseHtmlString(htmlString)
  16. if b then
  17. return Color(outColor.r, outColor.g, outColor.b, outColor.a)
  18. end
  19. return nil
  20. end
  21. ---@param htmlString string @ #ffffff
  22. function this.TryParseHtmlStringToColor(htmlString)
  23. ---@type Color
  24. local b, outColor = ColorUtility.TryParseHtmlString(htmlString)
  25. if b then
  26. return outColor
  27. end
  28. return nil
  29. end
  30. function this.GetColorText(msg, color)
  31. return string.format("<color=%s>%s</color>", color, msg)
  32. end
  33. function this.HexToRGB(str_num, outLine, hex)
  34. str_num = string.replace(str_num, "#", "")
  35. local str_num_six
  36. local rgba = {}
  37. if string.len(str_num) == 6 then
  38. str_num_six = str_num
  39. else
  40. str_num_six = string.sub(str_num, 1, 7)
  41. end
  42. rgba.r = tonumber(string.sub(str_num_six, 1, 2), hex) / 255
  43. rgba.g = tonumber(string.sub(str_num_six, 3, 4), hex) / 255
  44. rgba.b = tonumber(string.sub(str_num_six, 5, 6), hex) / 255
  45. rgba.a = (outLine or 255) / 255
  46. return rgba
  47. end
  48. function this.RichTextFormatToHex(color)
  49. return string.replace(color, '#', '0x') .. 'ff'
  50. end
  51. function this.RGB01ToHtmlString(r, g, b, a)
  52. r = math.ceil(r * 255)
  53. g = math.ceil(g * 255)
  54. b = math.ceil(b * 255)
  55. a = math.ceil(a * 255)
  56. return string.format("#%x%x%x%x", r, g, b, a)
  57. end