Advertisement
BigBlow_

ecran titre hexcolor

Nov 11th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. -- Fonction pour convertir un code couleur hexadécimal en RGB
  2. local function hexToRGB(hex)
  3. local r = tonumber(hex:sub(1, 2), 16)
  4. local g = tonumber(hex:sub(3, 4), 16)
  5. local b = tonumber(hex:sub(5, 6), 16)
  6. return r, g, b
  7. end
  8.  
  9. -- Parametres configurables
  10. local bgColorHex = "5b8d4c" -- Couleur de fond (hex)
  11. local textColorHex = "ffffff" -- Couleur du texte (hex)
  12. local title = "Mekanism Energie Factory" -- Texte a afficher, '\n' pour retour a la ligne si besoin
  13.  
  14. -- Initialisation du moniteur connecte automatiquement
  15. local mon = peripheral.find("monitor")
  16. if not mon then
  17. print("Aucun moniteur connecte n'a ete trouve.")
  18. return
  19. end
  20.  
  21. -- Conversion des couleurs hexadécimales en RGB
  22. local bgR, bgG, bgB = hexToRGB(bgColorHex)
  23. local textR, textG, textB = hexToRGB(textColorHex)
  24.  
  25. -- Configuration du moniteur
  26. mon.setBackgroundColor(colors.rgb(bgR, bgG, bgB))
  27. mon.setTextColor(colors.rgb(textR, textG, textB))
  28. mon.clear()
  29.  
  30. -- Fonction pour centrer et ajuster la taille du texte
  31. local function displayCenteredText(text)
  32. -- Diviser le texte en lignes selon les retours a la ligne
  33. local lines = {}
  34. for line in text:gmatch("([^\n]+)") do
  35. table.insert(lines, line)
  36. end
  37.  
  38. -- Commencer avec l'echelle maximale et reduire jusqu'a ce que le texte tienne sur l'ecran
  39. local scale = 5.0
  40. while scale >= 0.5 do
  41. mon.setTextScale(scale)
  42.  
  43. local width, height = mon.getSize()
  44.  
  45. -- Verifier si le texte tient dans l'ecran actuel
  46. local textFits = true
  47. for _, line in ipairs(lines) do
  48. if #line > width then
  49. textFits = false
  50. break
  51. end
  52. end
  53.  
  54. -- Si le texte tient dans les dimensions actuelles, arreter la boucle
  55. if textFits and #lines <= height then
  56. break
  57. else
  58. scale = scale - 0.1 -- Reduire l'echelle graduellement
  59. end
  60. end
  61.  
  62. -- Calcul des positions pour centrer chaque ligne de texte
  63. local width, height = mon.getSize()
  64. local yStart = math.floor((height - #lines) / 2) + 1
  65. for i, line in ipairs(lines) do
  66. local xPos = math.floor((width - #line) / 2) + 1
  67. mon.setCursorPos(xPos, yStart + i - 1)
  68. mon.write(line)
  69. end
  70. end
  71.  
  72. -- Affichage du texte centre
  73. displayCenteredText(title)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement