color.blade.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <div class="row mb-3 align-items-center">
  2. <label for="{{ $name }}" class="col-form-label @if(!($right ?? null)) col-md-4 text-md-end @endif">
  3. {{ $title ?? '' }}
  4. @isset($required) <sup>*</sup> @endisset
  5. </label>
  6. <div class="@if(!($right ?? null)) col-md-8 @endif ">
  7. <div class="input-group">
  8. <input type="color" name="{{ $name }}" id="{{ $name }}" style="height: 32px"
  9. class="form-control @error($name) is-invalid @enderror" @disabled($disabled ?? null) @required($required ?? null)
  10. @isset($min) min="{{ $min }}" @endisset
  11. @isset($max) max="{{ $max }}" @endisset
  12. @isset($pattern) pattern="{{ $pattern }}" @endisset
  13. placeholder="{{ $placeholder ?? ''}}"
  14. @isset($multiple) multiple="multiple" @endisset
  15. value="{{ old($name, $value ?? '') }}" autocomplete="{{ $name }}">
  16. <button class="btn btn-sm btn-outline-info" type="button" id="change-color">Рандомный цвет</button>
  17. </div>
  18. @error($name)
  19. <span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>
  20. @enderror
  21. </div>
  22. </div>
  23. @push('scripts')
  24. <script type="module">
  25. $('#bg_color').on('change input', function (){
  26. $('.avatars-select label:not(.disabled) img').css('background-color', $(this).val());
  27. });
  28. $('#change-color').on('click', function (){
  29. let h = Math.random() * 360;
  30. let s = (Math.random() * 20) + 70;
  31. let l = (Math.random() * 20) + 70;
  32. let color = hslToHex(h, s, l);
  33. $('#bg_color').val(color).trigger('change');
  34. });
  35. function hslToHex(h, s, l) {
  36. l /= 100;
  37. const a = s * Math.min(l, 1 - l) / 100;
  38. const f = n => {
  39. const k = (n + h / 30) % 12;
  40. const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
  41. return Math.round(255 * color).toString(16).padStart(2, '0'); // convert to Hex and prefix "0" if needed
  42. };
  43. return `#${f(0)}${f(8)}${f(4)}`;
  44. }
  45. </script>
  46. @endpush