مدیاویکی:Common.js: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
(function() { | (function() { | ||
function | function detectDarkMode() { | ||
const bgColor = window.getComputedStyle(document.body).backgroundColor; | |||
if (! | // تبدیل 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'); | |||
if ( | |||
body.classList.add('dark-mode'); | |||
} else { | } else { | ||
body.classList.remove('dark-mode'); | document.body.classList.remove('dark-mode'); | ||
} | } | ||
} | } | ||
// اجرا | // اجرا در شروع بارگذاری صفحه | ||
detectDarkMode(); | |||
// | // همچنین میتوان در صورت تغییر سایز یا تغییر تم با setInterval دوباره بررسی کرد (اختیاری) | ||
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);
})();