LoginSignup

Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

JS/TSでRGBをPIXI JSのColorの数値型に変換するには?

Posted at

JS/TSでRGBをPIXI JSのColorの数値型に変換するには?

解説

まず、RGB値のR,G,Bの値を"toString(16)"で16進数に変換し、
その16進数の値が1桁の場合は、先頭に0をつける。
最後に、"0x"に16進数のR,G,Bの値をくっつけて"parseInt"で数字に変換したら完成!

Javascript版

export function ToHex(R,G,B) {
    function hex(rgb) {
        const ret = rgb.toString(16);
        if (ret.length == 1) return "0" + ret;
        else return ret;
    }
    const r = hex(R);
    const g = hex(G);
    const b = hex(B);
    return parseInt("0x" + r + g + b);
}

Typescript版

export function ToHex(R:number,G:number,B:number) {
    function hex(rgb: number) {
        const ret = rgb.toString(16);
        if (ret.length == 1) return "0" + ret;
        else return ret;
    }
    const r = hex(R);
    const g = hex(G);
    const b = hex(B);
    return Number.parseInt("0x" + r + g + b);
}
0
0
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
Kimu1109

@Kimu1109(Kimu 1109)

主に.NETの開発を主にしている学生です。 ちなみにブロッコリーはマヨにつけて食べる派です。

Comments

hms97
@hms97

parseInt()の基数指定やビットシフトとビットORを使えば簡潔に書けますよ。

export function ToHex(R,G,B) {
  return parseInt(R, 16) << 16 | parseInt(G, 16) << 8 | parseInt(B, 16);
}
0

Let's comment your feelings that are more than good

Being held Article posting campaign

paiza×Qiita記事投稿キャンペーン「プログラミング問題をやってみて書いたコードを投稿しよう!」

~
View details
0
0

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address