| カラーコード変換 |
| HEXとRGB |
const b1 = document.getElementById("b1");
b1.addEventListener('click', () =>
{
//赤色をHEXに変更
convertCode(0, 0);
});
const b2 = document.getElementById("b2");
b2.addEventListener('click', () =>
{
//赤色をRGBに変更
convertCode(1, 0);
});
const b3 = document.getElementById("b3");
b3.addEventListener('click', () =>
{
//赤色をHEXに変更(透過色:50%)
convertCode(0, 1);
});
const b4 = document.getElementById("b4");
b4.addEventListener('click', () =>
{
//赤色をRGBに変更(透過色:50%)
convertCode(1, 1);
});
const code = document.getElementById("code");
const bkcolor = document.getElementById("bkcolor");
function convertCode(type, alpha)
{
const r = 255;
const g = 0;
const b = 0;
const a = alpha == 0 ? 1.0 : 0.5;
let rgbString = "";
if(type == 1)//rgba
{
rgbString = rgbaToHex(r, g, b, a, 1);//hex
rgbString = hex8ToRgba(rgbString);
}
else
{
rgbString = rgbaToHex(r, g, b, a, alpha);//hex
}
code.innerText = rgbString;
bkcolor.style.backgroundColor = rgbString;
}
function rgbaToHex(r, g, b, a, option)
{
const toHex = val =>
{
const h = val.toString(16).toUpperCase();
return h.length === 1 ? "0"+h : h;
};
// a: 0~1 を 0~255 に変換
const aa = Math.round(a * 255);
let tmp = "";
if(option == 0)
{
tmp = "#" + toHex(r) + toHex(g) + toHex(b);
}
else
{
tmp = "#" + toHex(r) + toHex(g) + toHex(b) + toHex(aa);
}
return tmp;
}
function hex8ToRgba(hex)
{
hex = hex.replace('#', '');
const r = parseInt(hex.slice(0,2), 16);
const g = parseInt(hex.slice(2,4), 16);
const b = parseInt(hex.slice(4,6), 16);
const a = parseInt(hex.slice(6,8), 16) / 255;
return `rgb(${r} ${g} ${b} / ${a.toFixed(2)})`;
}
| Color Code Conversion |
| HEX and RGB |
const b1 = document.getElementById("b1");
b1.addEventListener('click', () =>
{
//Change red to HEX
convertCode(0, 0);
});
const b2 = document.getElementById("b2");
b2.addEventListener('click', () =>
{
//Change red to RGB
convertCode(1, 0);
});
const b3 = document.getElementById("b3");
b3.addEventListener('click', () =>
{
//Change red to HEX (Transparent color: 50%)
convertCode(0, 1);
});
const b4 = document.getElementById("b4");
b4.addEventListener('click', () =>
{
//Change red to RGB (Transparent color: 50%)
convertCode(1, 1);
});
const code = document.getElementById("code");
const bkcolor = document.getElementById("bkcolor");
function convertCode(type, alpha)
{
const r = 255;
const g = 0;
const b = 0;
const a = alpha == 0 ? 1.0 : 0.5;
let rgbString = "";
if(type == 1)//rgba
{
rgbString = rgbaToHex(r, g, b, a, 1);//hex
rgbString = hex8ToRgba(rgbString);
}
else
{
rgbString = rgbaToHex(r, g, b, a, alpha);//hex
}
code.innerText = rgbString;
bkcolor.style.backgroundColor = rgbString;
}
function rgbaToHex(r, g, b, a, option)
{
const toHex = val =>
{
const h = val.toString(16).toUpperCase();
return h.length === 1 ? "0"+h : h;
};
// a: Convert 0-1 to 0-255
const aa = Math.round(a * 255);
let tmp = "";
if(option == 0)
{
tmp = "#" + toHex(r) + toHex(g) + toHex(b);
}
else
{
tmp = "#" + toHex(r) + toHex(g) + toHex(b) + toHex(aa);
}
return tmp;
}
function hex8ToRgba(hex)
{
hex = hex.replace('#', '');
const r = parseInt(hex.slice(0,2), 16);
const g = parseInt(hex.slice(2,4), 16);
const b = parseInt(hex.slice(4,6), 16);
const a = parseInt(hex.slice(6,8), 16) / 255;
return `rgb(${r} ${g} ${b} / ${a.toFixed(2)})`;
}
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |