6
7

More than 3 years have passed since last update.

RGBの補色を返す関数を書いた

Last updated at Posted at 2021-09-01

書き捨てのコードなので、適当に参考にしてください。
実装はTypeScriptですが、同じノリで他の言語もできると思ってます。

wikipedia
補色同士の色の組み合わせは、互いの色を引き立て合う相乗効果があり、これは「補色調和」といわれる。
しかし、純色など、明度が同じ補色同士を組合せた場合は、ハレーションを引き起こして、目がチカチカしてしまう
// FFFFFF, 00FF1E などの文字列を受け取り、その補色を設定して返却する
getComplementaryColor(rgbStr: string): string {
    const R = rgbStr.substr(0, 2).toLowerCase()
    const G = rgbStr.substr(2, 2).toLowerCase()
    const B = rgbStr.substr(4, 2).toLowerCase()

    const r = ('00' + (255 - parseInt(R, 16)).toString(16)).slice(-2)
    const g = ('00' + (255 - parseInt(G, 16)).toString(16)).slice(-2)
    const b = ('00' + (255 - parseInt(B, 16)).toString(16)).slice(-2)

    return r + g + b
}
6
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7