{"id":11786,"date":"2026-04-22T14:15:02","date_gmt":"2026-04-22T05:15:02","guid":{"rendered":"https:\/\/code-plus.jp\/gp\/?p=11786"},"modified":"2026-04-22T14:15:02","modified_gmt":"2026-04-22T05:15:02","slug":"contrast-colorjs-11786","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/contrast-colorjs-11786\/","title":{"rendered":"contrast-color()\u3092\u8a2d\u5b9a\u3067\u304d\u308bJavaScript\u3092\u4f5c\u3063\u3066\u307f\u305f\uff5c\u80cc\u666f\u8272\u304b\u3089\u6587\u5b57\u8272\u3092\u81ea\u52d5\u5224\u5b9a"},"content":{"rendered":"<!-- title: contrast-color()\u3092\u8a2d\u5b9a\u3067\u304d\u308bJavaScript\u3092\u4f5c\u3063\u3066\u307f\u305f\uff5c\u80cc\u666f\u8272\u304b\u3089\u6587\u5b57\u8272\u3092\u81ea\u52d5\u5224\u5b9a\ndescription: CSS\u306econtrast-color()\u306f\u307e\u3060\u5f15\u6570\u304c\u5fc5\u9808\u3067\u4e0d\u4fbf\u3002JavaScript\u3067--contrast-color\u5909\u6570\u3092\u81ea\u52d5\u30bb\u30c3\u30c8\u3057\u3001\u6587\u5b57\u8272\u3068hover\u8272\u3092\u80cc\u666f\u304b\u3089\u8a08\u7b97\u3059\u308b\u4ed5\u7d44\u307f\u3092\u7d39\u4ecb\u3002WCAG2.0\u8f1d\u5ea6\u5f0f\u30d9\u30fc\u30b9\u3001APCA\u5bfe\u5fdc\u6848\u3082\u89e3\u8aac\u3002\ntags: JavaScript, CSS, contrast-color, \u30ab\u30b9\u30bf\u30e0\u30d7\u30ed\u30d1\u30c6\u30a3, CSS\u5909\u6570, hover, \u914d\u8272, \u30a2\u30af\u30bb\u30b7\u30d3\u30ea\u30c6\u30a3, WCAG, APCA, getComputedStyle, requestAnimationFrame\ncategory: JavaScript -->\n\n<p>CSS \u306e <code>contrast-color()<\/code> \u95a2\u6570\u304c 2026 \u5e74 4 \u6708\u3001\u3088\u3046\u3084\u304f\u5168\u30e2\u30c0\u30f3\u30d6\u30e9\u30a6\u30b6\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u80cc\u666f\u8272\u3092\u6e21\u3059\u3068\u300c\u767d\u304b\u9ed2\u300d\u3069\u3061\u3089\u304b\u8aad\u307f\u3084\u3059\u3044\u65b9\u3092\u8fd4\u3057\u3066\u304f\u308c\u308b\u3068\u3044\u3046\u30b7\u30f3\u30d7\u30eb\u306a\u95a2\u6570\u3067\u3059\u3002<\/p>\n<p>\u3057\u304b\u3057\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b\u3068\u300c\u3053\u308c\u3001<b class=\"fw-600\">\u80cc\u666f\u8272\u3092\u660e\u793a\u7684\u306b\u6e21\u3055\u306a\u3044\u3068\u3044\u3051\u306a\u3044<\/b>\u306e\u304b\u2026\u2026\u300d\u3068\u6c17\u4ed8\u304d\u307e\u3059\u3002MDN \u306e\u30b5\u30f3\u30d7\u30eb\u3067\u3082\u3001\u30ab\u30e9\u30fc\u30d4\u30c3\u30ab\u30fc\u306e\u5024\u3092 JavaScript \u3067 CSS \u30ab\u30b9\u30bf\u30e0\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u4ee3\u5165\u3057\u3066\u304b\u3089 <code>contrast-color(var(--button-color))<\/code> \u3068\u66f8\u304f\u3001\u3068\u3044\u3046\u4f7f\u3044\u65b9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n<pre><code class=\"language-css\">:root {\n  --button-color: lightblue;\n}\n\nbutton {\n  background-color: var(--button-color);\n  color: contrast-color(var(--button-color));  \/* \u30c6\u30ad\u30b9\u30c8\u306e\u8272\u3092\u81ea\u52d5\u7684\u306b\u5bfe\u7167\u7684\u306b\u8a2d\u5b9a *\/\n}\n<\/code><\/pre>\n\n<p class=\"mt-1r\">\u300c\u81ea\u5206\u306e\u80cc\u666f\u8272\u3092\u53c2\u7167\u3059\u308b\u300d\u3060\u3051\u306e\u7528\u9014\u306a\u3089\u3001<code>color: contrast-color()<\/code> \u306e\u3088\u3046\u306b<b class=\"fw-600\">\u5f15\u6570\u306a\u3057\u3067\u66f8\u3051\u305f\u3089\u4fbf\u5229<\/b>\u306a\u306e\u3067\u3059\u304c\u3001\u73fe\u72b6\u306e\u4ed5\u69d8\u3067\u306f\u305d\u308c\u304c\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u3053\u3067\u4eca\u56de\u3001JavaScript \u3067 <code>--contrast-color<\/code> \u3068\u3044\u3046 CSS \u5909\u6570\u3092\u81ea\u52d5\u30bb\u30c3\u30c8\u3059\u308b\u4ed5\u7d44\u307f\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u3064\u3044\u3067\u306b <b class=\"fw-600\">hover \u6642\u306e\u80cc\u666f\u8272\u30fb\u6587\u5b57\u8272\u3082\u81ea\u52d5\u8a08\u7b97<\/b>\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n<!-- \u2605\u76ee\u6b21\u30ea\u30b9\u30c8 -->\n<ul class=\"tocList mt-1r\" data-toc-id=\"js-tocBWGD\" style=\"height:28rem\"><\/ul>\n<ins class=\"tocBtn is-fixed\" data-toc-id=\"js-tocBWGD\"><\/ins>\n\n\n\n\n\n\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">\u5b8c\u6210\u30a4\u30e1\u30fc\u30b8<\/h2>\n<p>CSS \u5074\u3067\u306f\u3053\u3046\u66f8\u3051\u307e\u3059\uff1a<\/p>\n<pre><code class=\"language-css\">.button {\n  background: #006571;\n  color: var(--contrast-color);            \/* \u80cc\u666f\u8272\u306b\u5bfe\u3057\u3066\u898b\u3084\u3059\u3044\u767d\/\u9ed2\u304c\u81ea\u52d5\u3067\u5165\u308b *\/\n}\n\n.button:hover {\n  background: var(--hover-bgcolor);        \/* hover\u6642\u306e\u80cc\u666f\u8272\u3082\u81ea\u52d5 *\/\n  color: var(--contrast-color);\n}\n\n.button.is-ghosted:hover {\n  background: var(--hover-color);          \/* \u900f\u660e\u80cc\u666f\u30dc\u30bf\u30f3\u7528\u306e\u8584\u3044tint *\/\n}\n<\/code><\/pre>\n<p>JavaScript \u306f\u5bfe\u8c61\u8981\u7d20\u3092 <code>querySelectorAll<\/code> \u3067\u62fe\u3063\u3066\u3001\u5404\u8981\u7d20\u306e\u80cc\u666f\u8272\u304b\u3089 <code>--contrast-color<\/code> \/ <code>--hover-bgcolor<\/code> \/ <code>--hover-color<\/code> \u3092\u8a08\u7b97\u3057\u3066 <code>element.style.setProperty()<\/code> \u3067\u66f8\u304d\u8fbc\u3080\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>\u5c06\u6765 <code>contrast-color()<\/code> \u304c\u3082\u3063\u3068\u666e\u53ca\u3057\u305f\u3089\u3001<code>var(--contrast-color)<\/code> \u3092 <code>contrast-color(currentBackground)<\/code> \u306e\u3088\u3046\u306a\u66f8\u304d\u65b9\u306b\u7f6e\u63db\u3059\u308c\u3070\u79fb\u884c\u3067\u304d\u307e\u3059\uff08\u304c\u3001\u73fe\u6642\u70b9\u3067\u306f\u307e\u3060 CSS \u3060\u3051\u3067\u306f\u66f8\u3051\u306a\u3044\u306e\u3067\u3001\u3053\u306e JS \u7248\u306e\u65b9\u304c\u4fbf\u5229\u3067\u3059\uff09\u3002<\/p>\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">\u30b3\u30fc\u30c9\u5168\u4f53<\/h2>\n<pre><code class=\"language-javascript\">(function () {\n  if (window._contrastColorInitialized) return;\n  window._contrastColorInitialized = true;\n\n  \/\/ RGB \u2192 HSL \u5909\u63db\n  const rgbToHsl = (r, g, b) =&gt; {\n    r \/= 255; g \/= 255; b \/= 255;\n    const max = Math.max(r, g, b);\n    const min = Math.min(r, g, b);\n    const l = (max + min) \/ 2;\n    let h = 0, s = 0;\n    if (max !== min) {\n      const d = max - min;\n      s = l &gt; 0.5 ? d \/ (2 - max - min) : d \/ (max + min);\n      switch (max) {\n        case r: h = ((g - b) \/ d + (g &lt; b ? 6 : 0)) \/ 6; break;\n        case g: h = ((b - r) \/ d + 2) \/ 6; break;\n        case b: h = ((r - g) \/ d + 4) \/ 6; break;\n      }\n    }\n    return &lsqb;Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)&rsqb;;\n  };\n\n  \/\/ \u8f1d\u5ea6\u8a08\u7b97\uff08WCAG 2.0 \u76f8\u5bfe\u8f1d\u5ea6\uff09\n  const getColorLuminance = (r, g, b) =&gt; {\n    const &lsqb;rs, gs, bs&rsqb; = &lsqb;r, g, b&rsqb;.map(c =&gt; {\n      c = c \/ 255;\n      return c &lt;= 0.03928 ? c \/ 12.92 : Math.pow((c + 0.055) \/ 1.055, 2.4);\n    });\n    return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;\n  };\n\n  \/\/ RGB\u6587\u5b57\u5217\u306e\u30d1\u30fc\u30b9\n  const parseRgb = (colorStr) =&gt; {\n    if (!colorStr || colorStr === &#39;transparent&#39;) return null;\n    const match = colorStr.match(\/&lsqb;\\d.&rsqb;+\/g);\n    if (!match) return null;\n    const &lsqb;r, g, b, a&rsqb; = match.map(Number);\n    if (a === 0) return null;\n    return &lsqb;r, g, b&rsqb;;\n  };\n\n  \/\/ hover\u8272\u8a08\u7b97\n  const calcHoverColor = (r, g, b, isDark) =&gt; {\n    const &lsqb;h, s, l&rsqb; = rgbToHsl(r, g, b);\n    let hoverS, hoverL;\n    if (isDark) {\n      hoverS = Math.round(s * 0.875);\n      hoverL = Math.min(l + 8, 100);\n    } else {\n      hoverS = Math.min(Math.round(s * 1.15), 100);\n      hoverL = Math.max(l - 8, 0);\n    }\n    return `hsl(${h}, ${hoverS}%, ${hoverL}%)`;\n  };\n\n  function applyContrastColor() {\n    const targets = document.querySelectorAll(\n      &#39;:is(.button, .buckle, .badge, .bauble, .bubble, .icon, .title, .caption, .box, .label, .alert)&#39;\n    );\n\n    targets.forEach(el =&gt; {\n      const style = window.getComputedStyle(el);\n      const bgRgb = parseRgb(style.backgroundColor);\n      const isGhosted = el.classList.contains(&#39;is-ghosted&#39;);\n\n      if (bgRgb) {\n        const &lsqb;r, g, b&rsqb; = bgRgb;\n        const luminance = getColorLuminance(r, g, b);\n        const isDark = luminance &lt;= 0.55;\n\n        el.style.setProperty(\n          &#39;--contrast-color&#39;,\n          isDark ? &#39;#FFF&#39; : &#39;var(--c-base-900, hsl(223, 6%, 13%))&#39;\n        );\n        el.style.setProperty(&#39;--hover-bgcolor&#39;, calcHoverColor(r, g, b, isDark));\n\n      } else if (isGhosted) {\n        const borderRgb = parseRgb(style.borderColor);\n        if (borderRgb) {\n          const &lsqb;r, g, b&rsqb; = borderRgb;\n          const luminance = getColorLuminance(r, g, b);\n          const isDark = luminance &lt;= 0.55;\n\n          el.style.setProperty(\n            &#39;--hover-color&#39;,\n            isDark\n              ? `rgba(${r}, ${g}, ${b}, 0.08)`\n              : calcHoverColor(r, g, b, false)\n          );\n        }\n      }\n    });\n  }\n\n  function initContrastColor() {\n    requestAnimationFrame(() =&gt; {\n      requestAnimationFrame(() =&gt; {\n        applyContrastColor();\n      });\n    });\n  }\n\n  if (document.readyState === &#39;loading&#39;) {\n    document.addEventListener(&#39;DOMContentLoaded&#39;, () =&gt; {\n      window.addEventListener(&#39;load&#39;, initContrastColor, { once: true });\n    }, { once: true });\n  } else if (document.readyState === &#39;interactive&#39;) {\n    window.addEventListener(&#39;load&#39;, initContrastColor, { once: true });\n  } else {\n    initContrastColor();\n  }\n})();\n<\/code><\/pre>\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">\u4f7f\u3044\u65b9<\/h2>\n<h3 class=\"fz-ml\">\u57fa\u672c\uff1a\u305d\u306e\u307e\u307e\u8aad\u307f\u8fbc\u3080\u3060\u3051<\/h3>\n<p>\u5bfe\u8c61\u30bb\u30ec\u30af\u30bf\u306e\u6761\u4ef6\u306b\u5408\u3046\u8981\u7d20\u3067\u3042\u308c\u3070\u3001\u30da\u30fc\u30b8\u8aad\u307f\u8fbc\u307f\u6642\u306b\u81ea\u52d5\u3067 CSS \u5909\u6570\u304c\u30bb\u30c3\u30c8\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-html\">&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;\n&lt;script src=&quot;_style-contrastColor.js&quot; defer&gt;&lt;\/script&gt;\n<\/code><\/pre>\n<h3 class=\"fz-ml\">\u5bfe\u8c61\u30bb\u30ec\u30af\u30bf\u3092\u5909\u66f4\u3059\u308b<\/h3>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f <code>.button<\/code>, <code>.badge<\/code>, <code>.alert<\/code> \u7b49\u3092\u5bfe\u8c61\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u81ea\u5206\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u5408\u308f\u305b\u3066 <code>querySelectorAll<\/code> \u306e\u5f15\u6570\u3092\u66f8\u304d\u63db\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u5909\u66f4\u524d\nconst targets = document.querySelectorAll(\n  &#39;:is(.button, .buckle, .badge, ...)&#39;\n);\n\n\/\/ \u5909\u66f4\u5f8c\uff08\u4f8b\uff1a.button, .badge \u306b\u3060\u3051\u9069\u7528\uff09\nconst targets = document.querySelectorAll(&#39;.button, .badge&#39;);\n<\/code><\/pre>\n<h3 class=\"fz-ml\">\u767d\u9ed2\u5224\u5b9a\u306e\u95be\u5024\u3092\u5909\u3048\u308b<\/h3>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u306f <code>luminance &lt;= 0.55<\/code> \u3067\u767d\u80cc\u666f\u3068\u9ed2\u80cc\u666f\u3092\u5207\u308a\u66ff\u3048\u3066\u3044\u307e\u3059\u3002\u5024\u3092\u5c0f\u3055\u304f\u3059\u308b\u3068\u300c\u767d\u6587\u5b57\u306b\u306a\u308b\u80cc\u666f\u300d\u304c\u5897\u3048\u3001\u5927\u304d\u304f\u3059\u308b\u3068\u300c\u9ed2\u6587\u5b57\u306b\u306a\u308b\u80cc\u666f\u300d\u304c\u5897\u3048\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">const isDark = luminance &lt;= 0.55;  \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\nconst isDark = luminance &lt;= 0.45;  \/\/ \u3088\u308a\u6697\u3044\u80cc\u666f\u3060\u3051\u767d\u6587\u5b57\u306b\nconst isDark = luminance &lt;= 0.65;  \/\/ \u3088\u308a\u591a\u304f\u306e\u80cc\u666f\u3067\u767d\u6587\u5b57\u306b\n<\/code><\/pre>\n<p>WCAG 2.0 \u306e\u8f1d\u5ea6\u5f0f\u306f\u4e2d\u9593\u660e\u5ea6\uff08\u304f\u3059\u3093\u3060\u9752\u3084\u7dd1\u306a\u3069\uff09\u3067\u5224\u5b9a\u304c\u63fa\u308c\u308b\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d1\u30ec\u30c3\u30c8\u306b\u5408\u308f\u305b\u3066\u5fae\u8abf\u6574\u3059\u308b\u3068\u826f\u3044\u3067\u3059\u3002<\/p>\n<h3 class=\"fz-ml\">hover \u8272\u306e\u6fc3\u3055\u3092\u8abf\u6574\u3059\u308b<\/h3>\n<p><code>calcHoverColor<\/code> \u95a2\u6570\u3067 HSL \u306e\u660e\u5ea6\u3092\u4e0a\u4e0b\u3055\u305b\u3066\u3044\u307e\u3059\u3002<code>+8<\/code> \/ <code>-8<\/code> \u306e\u90e8\u5206\u3092\u5897\u6e1b\u3059\u308b\u3068 hover \u6642\u306e\u5909\u5316\u91cf\u304c\u5909\u308f\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">hoverL = Math.min(l + 8, 100);   \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\uff08\u63a7\u3048\u3081\u306a\u5909\u5316\uff09\nhoverL = Math.min(l + 15, 100);  \/\/ \u306f\u3063\u304d\u308a\u3057\u305f\u5909\u5316\n<\/code><\/pre>\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">\u30b3\u30fc\u30c9\u306e\u304b\u3093\u305f\u3093\u89e3\u8aac<\/h2>\n<h3 class=\"fz-ml\">1. \u80cc\u666f\u8272\u306e\u53d6\u5f97\u306f <code>getComputedStyle<\/code><\/h3>\n<p>\u30a4\u30f3\u30e9\u30a4\u30f3\u30b9\u30bf\u30a4\u30eb\u3060\u3051\u3067\u306a\u304f\u3001CSS \u30d5\u30a1\u30a4\u30eb\u3067\u6307\u5b9a\u3055\u308c\u305f\u80cc\u666f\u8272\u3082\u542b\u3081\u3066<b class=\"fw-600\">\u5b9f\u969b\u306b\u9069\u7528\u3055\u308c\u3066\u3044\u308b\u5024<\/b>\u3092\u53d6\u5f97\u3057\u305f\u3044\u306e\u3067 <code>window.getComputedStyle(el).backgroundColor<\/code> \u3092\u4f7f\u3044\u307e\u3059\u3002\u623b\u308a\u5024\u306f <code>rgb(2, 127, 65)<\/code> \u306e\u3088\u3046\u306a\u6587\u5b57\u5217\u306b\u306a\u308b\u306e\u3067\u3001\u6b63\u898f\u8868\u73fe\u3067\u6570\u5024\u3092\u53d6\u308a\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3 class=\"fz-ml\">2. \u8f1d\u5ea6\u306f WCAG 2.0 \u306e\u76f8\u5bfe\u8f1d\u5ea6\u5f0f<\/h3>\n<pre><code class=\"language-javascript\">return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;\n<\/code><\/pre>\n<p>RGB \u306e\u5404\u30c1\u30e3\u30f3\u30cd\u30eb\u3092 sRGB \u30ac\u30f3\u30de\u3067\u7dda\u5f62\u5316\u3057\u3066\u304b\u3089\u4fc2\u6570\u3092\u304b\u3051\u3066\u5408\u8a08\u3059\u308b\u3068\u3044\u3046\u6a19\u6e96\u7684\u306a\u8a08\u7b97\u3067\u3059\u3002\u7dd1\u304c\u4e00\u756a\u660e\u308b\u304f\u611f\u3058\u3089\u308c\u308b\uff08\u4eba\u9593\u306e\u76ee\u304c\u7dd1\u306b\u654f\u611f\uff09\u306e\u3067 <code>0.7152<\/code> \u3068\u91cd\u307f\u304c\u5927\u304d\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3 class=\"fz-ml\">3. hover \u8272\u306f HSL \u306e\u660e\u5ea6\u3092\u4e0a\u4e0b<\/h3>\n<p>RGB \u306e\u307e\u307e\u300c\u5c11\u3057\u6697\u304f\u300d\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u8272\u76f8\u304c\u305a\u308c\u308b\u306e\u3067\u3001\u4e00\u5ea6 HSL \u306b\u5909\u63db\u3057\u3066 <b class=\"fw-600\">L\uff08\u660e\u5ea6\uff09\u3060\u3051\u3092 8% \u4e0a\u4e0b<\/b>\u3055\u305b\u3066\u3044\u307e\u3059\u3002\u6697\u3044\u8272\u306f\u5c11\u3057\u660e\u308b\u304f\u3001\u660e\u308b\u3044\u8272\u306f\u5c11\u3057\u6697\u304f\u3001\u3068\u3044\u3046\u81ea\u7136\u306a hover \u5909\u5316\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3 class=\"fz-ml\">4. <code>requestAnimationFrame<\/code> \u3092 2\u6bb5\u91cd\u306d<\/h3>\n<pre><code class=\"language-javascript\">requestAnimationFrame(() =&gt; {\n  requestAnimationFrame(() =&gt; {\n    applyContrastColor();\n  });\n});\n<\/code><\/pre>\n<p>CSS \u30ab\u30b9\u30bf\u30e0\u30d7\u30ed\u30d1\u30c6\u30a3\uff08<code>var(--c-primary)<\/code> \u7b49\uff09\u304c\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u304b\u3089\u89e3\u6c7a\u3055\u308c\u308b\u306e\u3092 1 \u30d5\u30ec\u30fc\u30e0\u5f85\u3061\u3001\u3055\u3089\u306b <code>getComputedStyle<\/code> \u304c\u6700\u65b0\u5024\u3092\u8fd4\u3059\u306e\u3092\u5f85\u3064\u305f\u3081\u306b 2 \u6bb5\u91cd\u306d\u306b\u3057\u3066\u3044\u307e\u3059\u30021 \u6bb5\u3060\u3051\u3060\u3068 <code>getComputedStyle<\/code> \u304c\u53e4\u3044\u5024\u3092\u8fd4\u3059\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001\u5b89\u5168\u7b56\u3068\u3057\u3066 2 \u6bb5\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<h3 class=\"fz-ml\">5. <code>.is-ghosted<\/code>\uff08\u900f\u660e\u80cc\u666f\uff09\u306e\u7279\u5225\u51e6\u7406<\/h3>\n<p><code>background: transparent<\/code> \u306e\u30dc\u30bf\u30f3\u306f\u80cc\u666f\u8272\u304b\u3089\u8f1d\u5ea6\u3092\u8a08\u7b97\u3067\u304d\u306a\u3044\u306e\u3067\u3001\u4ee3\u308f\u308a\u306b <code>borderColor<\/code> \u3092\u898b\u3066\u3044\u307e\u3059\u3002hover \u6642\u306f<b class=\"fw-600\">\u30dc\u30fc\u30c0\u30fc\u8272\u3092\u8584\u304f\u5857\u308b<\/b>\u6319\u52d5\u306b\u3057\u305f\u3044\u306e\u3067\u3001\u6697\u3044\u30dc\u30fc\u30c0\u30fc\u306a\u3089 <code>rgba(r, g, b, 0.08)<\/code>\u3001\u660e\u308b\u3044\u30dc\u30fc\u30c0\u30fc\u306a\u3089 HSL \u8abf\u6574\u7248\u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">APCA \u306b\u3064\u3044\u3066\uff08\u4f59\u8ac7\uff09<\/h2>\n<p>\u4eca\u56de\u306f WCAG 2.0 \u306e\u8f1d\u5ea6\u5f0f\u3092\u4f7f\u3044\u307e\u3057\u305f\u304c\u3001\u6700\u8fd1\u306f <b class=\"fw-600\">APCA\uff08Accessible Perceptual Contrast Algorithm\uff09<\/b> \u3068\u3044\u3046\u65b0\u3057\u3044\u30b3\u30f3\u30c8\u30e9\u30b9\u30c8\u8a08\u7b97\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u304c\u6ce8\u76ee\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>WCAG 2.0 \u306e\u8f1d\u5ea6\u5f0f\u306b\u306f\u300c\u6697\u3044\u80cc\u666f\u3067\u306e\u8aa4\u5224\u5b9a\u304c\u591a\u3044\u300d\u300c\u4e2d\u9593\u660e\u5ea6\u3067\u9ed2\u6587\u5b57\u3092\u63a8\u5968\u3059\u308b\u304c\u5b9f\u969b\u306f\u767d\u306e\u65b9\u304c\u8aad\u307f\u3084\u3059\u3044\u300d\u306a\u3069\u306e\u6279\u5224\u304c\u3042\u308a\u3001APCA \u306f\u305d\u308c\u3089\u3092\u6539\u5584\u3057\u305f\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3067\u3059\u3002<b class=\"fw-600\">WCAG 3 \u306e\u5019\u88dc\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0<\/b>\u3068\u3057\u3066\u4ed5\u69d8\u7b56\u5b9a\u304c\u9032\u3081\u3089\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3 class=\"fz-ml\">WCAG 2.0 \u3068\u306e\u9055\u3044<\/h3>\n<ul style=\"list-style-type:bullet;\" class=\"my-0.5r\">\n<li>WCAG 2.0\uff1a<b class=\"fw-600\">\u8f1d\u5ea6\u6bd4<\/b>\uff08\u5358\u7d14\u306a\u6bd4\u7387\uff09\u3067\u5224\u5b9a\u3001\u95be\u5024 4.5:1 \u306a\u3069<\/li>\n<li>APCA\uff1a<b class=\"fw-600\">lightness contrast\uff08Lc\uff09<\/b>\u3068\u3044\u3046\u72ec\u81ea\u306e\u6570\u5024\u3092\u8a08\u7b97\u3001\u95be\u5024 \u00b145\u301c75 \u306a\u3069<\/li>\n<li>APCA \u306f\u30c6\u30ad\u30b9\u30c8\u3068\u80cc\u666f\u306e<b class=\"fw-600\">\u6975\u6027\uff08\u3069\u3061\u3089\u304c\u6697\u3044\u304b\uff09<\/b>\u3092\u8003\u616e\u3059\u308b<\/li>\n<\/ul>\n<h3 class=\"fz-ml\">\u5207\u66ff\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u306a\u3089<\/h3>\n<p>\u3082\u3057\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3092\u9078\u3079\u308b\u3088\u3046\u306b\u3059\u308b\u306a\u3089\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8a2d\u8a08\u304c\u8003\u3048\u3089\u308c\u307e\u3059\uff1a<\/p>\n<pre><code class=\"language-javascript\">const options = {\n  algorithm: &#39;wcag&#39;,           \/\/ &#39;wcag&#39; | &#39;apca&#39;\n  threshold: {\n    wcag: 0.55,                \/\/ WCAG \u7528\u306e\u8f1d\u5ea6\u95be\u5024\n    apca: 45                   \/\/ APCA \u7528\u306e Lc \u95be\u5024\n  }\n};\n\nfunction isDarkBackground(r, g, b, opts) {\n  if (opts.algorithm === &#39;apca&#39;) {\n    return calcAPCA(r, g, b) &gt;= opts.threshold.apca;\n  }\n  return getColorLuminance(r, g, b) &lt;= opts.threshold.wcag;\n}\n<\/code><\/pre>\n<p>\u305f\u3060\u3001APCA \u306f<b class=\"fw-600\">\u4ed5\u69d8\u304c\u4f55\u5ea6\u304b\u66f4\u65b0\u3055\u308c\u3066\u3044\u308b<\/b>\uff08G-4g, G-4h \u7b49\uff09\u3053\u3068\u3068\u3001<b class=\"fw-600\">\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u5b9f\u88c5\uff08apca-w3\uff09\u304c AGPL 3.0 \u30e9\u30a4\u30bb\u30f3\u30b9<\/b>\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002\u5b9f\u88c5\u3059\u308b\u5834\u5408\u306f\u30e9\u30a4\u30bb\u30f3\u30b9\u306b\u611f\u67d3\u3057\u306a\u3044\u3088\u3046<b class=\"fw-600\">\u6570\u5f0f\u3092\u53c2\u8003\u306b\u81ea\u529b\u5b9f\u88c5<\/b>\u3059\u308b\u304b\u3001MIT \u4e92\u63db\u306e\u6d3e\u751f\u5b9f\u88c5\u3092\u63a2\u3059\u306e\u304c\u7121\u96e3\u3067\u3059\u3002<\/p>\n<p>\u51e6\u7406\u901f\u5ea6\u306f WCAG 2.0 \u306e\u7d04 2\u301c3 \u500d\u306e\u6f14\u7b97\u91cf\u3067\u3059\u304c\u30011 \u8981\u7d20\u3042\u305f\u308a\u6570\u30de\u30a4\u30af\u30ed\u79d2\u30ec\u30d9\u30eb\u306a\u306e\u3067\u4f53\u611f\u5dee\u306f\u51fa\u307e\u305b\u3093\uff08\u5b9f\u969b\u306e\u30dc\u30c8\u30eb\u30cd\u30c3\u30af\u306f <code>getComputedStyle<\/code> \/ <code>setProperty<\/code> \u7b49\u306e DOM \u64cd\u4f5c\u5074\uff09\u3002<\/p>\n<p>\u3072\u3068\u307e\u305a\u300c\u767d\u80cc\u666f\u306a\u3089\u9ed2\u6587\u5b57\u3001\u9ed2\u80cc\u666f\u306a\u3089\u767d\u6587\u5b57\u300d\u3068\u3044\u3046\u5358\u7d14\u306a\u7528\u9014\u3067\u306f WCAG 2.0 \u3067\u3082\u5341\u5206\u5224\u5b9a\u3067\u304d\u308b\u306e\u3067\u3001\u4eca\u56de\u306f\u305d\u306e\u307e\u307e\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u30d1\u30ec\u30c3\u30c8\u3067\u8aa4\u5224\u5b9a\u304c\u51fa\u308b\u3088\u3046\u306a\u3089 APCA \u5316\u3092\u691c\u8a0e\u3059\u308b\u3001\u3068\u3044\u3046\u3050\u3089\u3044\u3067\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n\n<hr class=\"separator mt-3r\">\n\n<h2 class=\"fz-xl\">\u307e\u3068\u3081<\/h2>\n<ul style=\"list-style-type:bullet;\" class=\"my-0.5r\">\n<li>CSS <code>contrast-color()<\/code> \u306f\u4fbf\u5229\u3060\u304c\u300c<b class=\"fw-600\">\u80cc\u666f\u8272\u3092\u660e\u793a\u7684\u306b\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308b<\/b>\u300d\u3068\u3044\u3046\u5f31\u70b9\u304c\u3042\u308b<\/li>\n<li>JavaScript \u3067 <code>--contrast-color<\/code> \u5909\u6570\u3092\u81ea\u52d5\u30bb\u30c3\u30c8\u3059\u308c\u3070\u3001CSS \u5074\u306f <code>color: var(--contrast-color)<\/code> \u3068\u66f8\u304f\u3060\u3051\u3067\u6e08\u3080<\/li>\n<li>\u3064\u3044\u3067\u306b <code>--hover-bgcolor<\/code> \/ <code>--hover-color<\/code> \u3082\u81ea\u52d5\u8a08\u7b97\u3059\u308b\u3068\u3001hover \u306e\u8a18\u8ff0\u3082\u30c8\u30fc\u30af\u30f3\u5316\u3067\u304d\u308b<\/li>\n<li>\u5c06\u6765 CSS \u3060\u3051\u3067\u5b8c\u7d50\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3089\u3001<code>var(--contrast-color)<\/code> \u2192 <code>contrast-color()<\/code> \u3078\u306e\u79fb\u884c\u3082\u697d<\/li>\n<\/ul>\n<p>\u30c7\u30b6\u30a4\u30f3\u30b7\u30b9\u30c6\u30e0\u5074\u3067\u30dc\u30bf\u30f3\u8272\u306e\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\uff08<code>.is-primary<\/code>, <code>.is-accent<\/code> \u306a\u3069\uff09\u3092 CSS \u5909\u6570\u3067\u5b9a\u7fa9\u3057\u3066\u3044\u308c\u3070\u3001\u3053\u306e JS \u306f<b class=\"fw-600\">\u3069\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u3082\u305d\u306e\u307e\u307e\u4f7f\u3044\u56de\u305b\u308b<\/b>\u306f\u305a\u3067\u3059\u3002\u95be\u5024\u3084\u5bfe\u8c61\u30bb\u30ec\u30af\u30bf\u3060\u3051\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3054\u3068\u306b\u8abf\u6574\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>","protected":false},"excerpt":{"rendered":"CSS\u306econtrast-color()\u306f\u307e\u3060\u5f15\u6570\u304c\u5fc5\u9808\u3067\u4e0d\u4fbf\u3002JavaScript\u3067--contrast-color\u5909\u6570\u3092\u81ea\u52d5\u30bb\u30c3\u30c8\u3057\u3001\u6587\u5b57\u8272\u3068hover\u8272\u3092\u80cc\u666f\u304b\u3089\u8a08\u7b97\u3059\u308b\u4ed5\u7d44\u307f\u3092\u7d39\u4ecb\u3002WCAG2.0\u8f1d\u5ea6\u5f0f\u30d9\u30fc\u30b9\u3001APCA\u5bfe\u5fdc\u6848\u3082\u89e3\u8aac\u3002","protected":false},"author":1,"featured_media":11789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[8],"tags":[761,760,762,763,764,765,766,346,178,148,81],"group":[],"pg":[],"class_list":["post-11786","post","type-post","status-publish","format-standard","has-post-thumbnail","category-javascript","tag-apca","tag-getcomputedstyle","tag-wcag","tag-763","tag-hover","tag-765","tag-contrast-color","tag-requestanimationframe","tag-178","tag-javascript","tag-css"],"acf":[],"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/11786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/comments?post=11786"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/11786\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media\/11789"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=11786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=11786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=11786"},{"taxonomy":"group","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/group?post=11786"},{"taxonomy":"pg","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/pg?post=11786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}