پرش به محتوا

مدیاویکی:Common.js: تفاوت میان نسخه‌ها

از دانشنامه دوبله و صداگذاری ایران
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
 
خط ۱: خط ۱:
(function() {
(function() {
   function applyDarkModeClass() {
   function detectDarkMode() {
     var body = document.body;
     const bgColor = window.getComputedStyle(document.body).backgroundColor;
     if (!body) return;
    // تبدیل rgb(34, 34, 34) به [34,34,34]
    const rgb = bgColor.match(/\d+/g);
     if (!rgb) return;


     // خواندن رنگ پس‌زمینه صفحه
     // محاسبه روشنایی نسبی (luminance)
     var bgColor = window.getComputedStyle(body).backgroundColor;
     const r = parseInt(rgb[0], 10);
     if (!bgColor) return;
    const g = parseInt(rgb[1], 10);
     const b = parseInt(rgb[2], 10);
    const luminance = 0.2126*r + 0.7152*g + 0.0722*b;


    // تبدیل به مقادیر عددی RGB
     if (luminance < 128) {
    var rgb = bgColor.match(/\d+/g);
       document.body.classList.add('dark-mode');
    if (!rgb || rgb.length < 3) return;
 
    var r = parseInt(rgb[0], 10);
    var g = parseInt(rgb[1], 10);
    var b = parseInt(rgb[2], 10);
 
    // فرمول روشنایی (Brightness)
    var brightness = (r * 299 + g * 587 + b * 114) / 1000;
 
    // اگر روشنایی کمتر از 128 بود حالت تاریک در نظر گرفته شود
     if (brightness < 128) {
       body.classList.add('dark-mode');
     } else {
     } else {
       body.classList.remove('dark-mode');
       document.body.classList.remove('dark-mode');
     }
     }
   }
   }


   // اجرا هنگام بارگذاری صفحه
   // اجرا در شروع بارگذاری صفحه
   window.addEventListener('load', applyDarkModeClass);
   detectDarkMode();


   // و در صورت تغییر حالت رنگ (اختیاری)
   // همچنین می‌توان در صورت تغییر سایز یا تغییر تم با setInterval دوباره بررسی کرد (اختیاری)
   // window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', applyDarkModeClass);
   window.addEventListener('resize', detectDarkMode);
})();
})();

نسخهٔ کنونی تا ‏۱۱ ژوئن ۲۰۲۵، ساعت ۲۳:۱۸

(function() {
  function detectDarkMode() {
    const bgColor = window.getComputedStyle(document.body).backgroundColor;
    // تبدیل rgb(34, 34, 34) به [34,34,34]
    const rgb = bgColor.match(/\d+/g);
    if (!rgb) return;

    // محاسبه روشنایی نسبی (luminance)
    const r = parseInt(rgb[0], 10);
    const g = parseInt(rgb[1], 10);
    const b = parseInt(rgb[2], 10);
    const luminance = 0.2126*r + 0.7152*g + 0.0722*b;

    if (luminance < 128) {
      document.body.classList.add('dark-mode');
    } else {
      document.body.classList.remove('dark-mode');
    }
  }

  // اجرا در شروع بارگذاری صفحه
  detectDarkMode();

  // همچنین می‌توان در صورت تغییر سایز یا تغییر تم با setInterval دوباره بررسی کرد (اختیاری)
  window.addEventListener('resize', detectDarkMode);
})();