color.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. function hexToRgb($hex) {
  3. $hex = ltrim($hex, '#');
  4. if (strlen($hex) === 3) {
  5. $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
  6. $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
  7. $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
  8. } else {
  9. $r = hexdec(substr($hex, 0, 2));
  10. $g = hexdec(substr($hex, 2, 2));
  11. $b = hexdec(substr($hex, 4, 2));
  12. }
  13. return array('r' => $r, 'g' => $g, 'b' => $b);
  14. }
  15. function rgbToHex($r, $g, $b) {
  16. $hex = sprintf("#%02X%02X%02X", $r, $g, $b);
  17. return $hex;
  18. }
  19. function getComplementHex($hex) {
  20. $rgb = hexToRgb($hex);
  21. $compR = 255 - $rgb['r'];
  22. $compG = 255 - $rgb['g'];
  23. $compB = 255 - $rgb['b'];
  24. return rgbToHex($compR, $compG, $compB);
  25. }
  26. $original_hex = "#000000";
  27. $complement_hex = "#FFFFFF";
  28. if (isset($_POST['calculate'])) {
  29. if (!empty($_POST['hex_color']) && preg_match('/^#([0-9A-F]{3}|[0-9A-F]{6})$/i', $_POST['hex_color'])) {
  30. $original_hex = strtoupper($_POST['hex_color']);
  31. $complement_hex = getComplementHex($original_hex);
  32. } else if (isset($_POST['r'], $_POST['g'], $_POST['b'])) {
  33. $r = max(0, min(255, intval($_POST['r'])));
  34. $g = max(0, min(255, intval($_POST['g'])));
  35. $b = max(0, min(255, intval($_POST['b'])));
  36. $original_hex = rgbToHex($r, $g, $b);
  37. $complement_hex = getComplementHex($original_hex);
  38. }
  39. }
  40. ?>
  41. <!DOCTYPE html>
  42. <html lang="de">
  43. <head>
  44. <meta charset="UTF-8">
  45. <title>Komplementärfarbe berechnen</title>
  46. <style>
  47. body {
  48. font-family: Arial, sans-serif;
  49. margin: 20px;
  50. }
  51. .color-box {
  52. width: 100px;
  53. height: 100px;
  54. display: inline-block;
  55. margin: 10px;
  56. border: 1px solid #000;
  57. }
  58. .input-group {
  59. margin-bottom: 20px;
  60. }
  61. .result {
  62. margin-top: 30px;
  63. padding: 20px;
  64. border: 1px solid #ddd;
  65. border-radius: 5px;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <h1>Komplementärfarbe berechnen</h1>
  71. <form method="post">
  72. <div class="input-group">
  73. <label for="hex_color">HEX Farbcode:</label>
  74. <input type="text" id="hex_color" name="hex_color" maxlength="7" placeholder="#000000" value="<?= htmlspecialchars($original_hex) ?>" oninput="updateColor()">
  75. </div>
  76. <div class="input-group">
  77. <label>RGB Schieberegler:</label>
  78. <div>
  79. <label for="r">Rot: <span id="red-value"><?= hexdec(substr($original_hex, 1, 2)) ?></span></label>
  80. <input type="range" id="r" name="r" min="0" max="255" value="<?= hexdec(substr($original_hex, 1, 2)) ?>" oninput="updateFromSliders()">
  81. </div>
  82. <div>
  83. <label for="g">Grün: <span id="green-value"><?= hexdec(substr($original_hex, 3, 2)) ?></span></label>
  84. <input type="range" id="g" name="g" min="0" max="255" value="<?= hexdec(substr($original_hex, 3, 2)) ?>" oninput="updateFromSliders()">
  85. </div>
  86. <div>
  87. <label for="b">Blau: <span id="blue-value"><?= hexdec(substr($original_hex, 5, 2)) ?></span></label>
  88. <input type="range" id="b" name="b" min="0" max="255" value="<?= hexdec(substr($original_hex, 5, 2)) ?>" oninput="updateFromSliders()">
  89. </div>
  90. </div>
  91. <div class="input-group">
  92. <label for="color_picker">Farbwähler:</label>
  93. <input type="color" id="color_picker" name="color_picker" value="<?= $original_hex ?>" oninput="updateColorPicker()">
  94. </div>
  95. <div class="input-group">
  96. <button type="submit" name="calculate">Berechnen</button>
  97. </div>
  98. </form>
  99. <div class="result">
  100. <h3>Originalfarbe:</h3>
  101. <div class="color-box" style="background-color: <?= $original_hex; ?>;"></div>
  102. <p>HEX: <?= htmlspecialchars($original_hex); ?></p>
  103. <h3>Komplementärfarbe:</h3>
  104. <div class="color-box" style="background-color: <?= $complement_hex; ?>;"></div>
  105. <p>HEX: <?= htmlspecialchars($complement_hex); ?></p>
  106. </div>
  107. <script>
  108. function updateColor() {
  109. const hexInput = document.getElementById('hex_color').value;
  110. if (/^#([0-9A-F]{3}|[0-9A-F]{6})$/i.test(hexInput)) {
  111. const r = parseInt(hexInput.substr(1, 2), 16);
  112. const g = parseInt(hexInput.substr(3, 2), 16);
  113. const b = parseInt(hexInput.substr(5, 2), 16);
  114. document.getElementById('r').value = r;
  115. document.getElementById('g').value = g;
  116. document.getElementById('b').value = b;
  117. document.getElementById('red-value').textContent = r;
  118. document.getElementById('green-value').textContent = g;
  119. document.getElementById('blue-value').textContent = b;
  120. document.getElementById('color_picker').value = hexInput.toLowerCase();
  121. }
  122. }
  123. function updateFromSliders() {
  124. const r = document.getElementById('r').value;
  125. const g = document.getElementById('g').value;
  126. const b = document.getElementById('b').value;
  127. let hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  128. if (hex.length === 4) {
  129. hex = "#" + [hex[1], hex[1], hex[2], hex[2], hex[3], hex[3]].join("");
  130. }
  131. document.getElementById('hex_color').value = hex.toUpperCase();
  132. document.getElementById('color_picker').value = hex.toLowerCase();
  133. document.getElementById('red-value').textContent = r;
  134. document.getElementById('green-value').textContent = g;
  135. document.getElementById('blue-value').textContent = b;
  136. }
  137. function updateColorPicker() {
  138. const color = document.getElementById('color_picker').value;
  139. const r = Math.round(parseInt(color.substr(1, 2), 16));
  140. const g = Math.round(parseInt(color.substr(3, 2), 16));
  141. const b = Math.round(parseInt(color.substr(5, 2), 16));
  142. document.getElementById('hex_color').value = color.toUpperCase();
  143. document.getElementById('r').value = r;
  144. document.getElementById('g').value = g;
  145. document.getElementById('b').value = b;
  146. document.getElementById('red-value').textContent = r;
  147. document.getElementById('green-value').textContent = g;
  148. document.getElementById('blue-value').textContent = b;
  149. }
  150. </script>
  151. </body>
  152. </html>