プログラミングとイラストレーション » RGB値を取得|HTML5
プログラミングとイラストレーション > HTML5 > RGB値を取得|HTML5

RGB値を取得|HTML5

RGB HTML5

HTML5, JavaScriptでRGB値を取得する : デモ

HTML5, JavaScriptでRGB値を取得する : ZIPファイル(9kb)

上記のデモページで、PC上でマウスオーバーか、またはスマートフォンでは、タッチイベントで、画像のRGB値を取得できます。

1.HTML

<!DOCTYPE html>
<html lang="jp">
    <head>
        <meta charset="UTF-8" />
        <title>RGB値取得</title>
        <link href="css/base.css" rel="stylesheet" type="text/css">
        <script src="js/devicecheck.js"></script>
        <script src="js/base.js"></script>
    </head>
    <body>
        <div id="outline" width="275" height="275">
            <canvas id="canvas" width="1530" height="275"></canvas>
            <div id="text" width="100" height="20"></div>
        </div>
    </body>
</html>

2.CSS

@charset "utf-8";
  
body
{
    margin: 0;
    padding: 0;
    background-color: #fff;
}

#text
{
    position: absolute;
    top: 10px;
    left: 10px;
    color: #fff;
}

#outline, #canvas
{
	position: absolute;
    top: 0;
    left: 0;
    width: 275px;
    height: 275px;
}

#outline
{
	border: solid 1px #000000;
}
  
#canvas
{
    width: 1530px;
    /*background-color: #fff;*/
}

3.JavaScript

devicecheck.js

function Sandbox()
{
}
	
Sandbox.prototype = 
{
	/*name : "My App",
	version : "1.0",
	getName : function()
	{
		return this.name;
	}*/
};

Sandbox.prototype.g_mobiledevice = false;
Sandbox.prototype.g_browsername = "unknown";
Sandbox.prototype.g_browserver = -1;

if(checkUserAgent() == false)
{
	window.alert('このブラウザは対象外です');
}

function checkUserAgent()
{
	var ua = navigator.userAgent;

	//iPhoneか?	
	var mstr = ua.match(/iPhone OS \d+/);
	if(mstr != null){
		var vstr = mstr[0].match(/\d+/);
		if(parseInt(vstr[0]) >= 3) 
		{
			Sandbox.prototype.g_mobiledevice = true;
			Sandbox.prototype.g_browsername = 'iPhone';
			//alert('major-version ' + vstr);
			return true;
		}
	}
	//iPadか?
	if(ua.indexOf('iPad') > -1)
	{
		mstr = ua.match(/CPU OS \d+/);
		if(mstr != null)
		{
			var vstr = mstr[0].match(/\d+/);
			if(parseInt(vstr[0]) >= 3) {
				Sandbox.prototype.g_mobiledevice = true;
				Sandbox.prototype.g_browsername = 'iPad';
				//alert('major-version ' + vstr);
				return true;
			}		
		}		
	}	
	//Androidか?	
	var mstr = ua.match(/Android \d+\.\d+/);
	if(mstr != null)
	{
		Sandbox.prototype.g_browsername = 'Android';
		var vstr = mstr[0].match(/\d+\.\d+/);
		Sandbox.prototype.g_browserver = parseFloat(vstr[0]);
		Sandbox.prototype.g_mobiledevice = true;
		if(Sandbox.prototype.g_browserver > 2.1) 
		{
			//alert('version ' + vstr);
			return true;
		}
	}
	//Chromeか?
	mstr = ua.match(/Chrome\/\d+/);
	if(mstr != null)
	{
		Sandbox.prototype.g_browsername = 'Chrome';
		var vstr = mstr[0].match(/\d+/);
		Sandbox.prototype.g_browserver = parseInt(vstr[0]);
		if(Sandbox.prototype.g_browserver >= 9) 
		{
			//alert('major-version ' + vstr);
			
			return true;
		}		
	}
	//Safariか?
	if(ua.indexOf('Safari') > -1)
	{
		mstr = ua.match(/Version\/\d+/);
		if(mstr != null){
			var vstr = mstr[0].match(/\d+/);
			if(parseInt(vstr[0]) >= 5) 
			{
				Sandbox.prototype.g_browsername = 'Safari';
				//alert('major-version ' + vstr);
				return true;
			}		
		}		
	}
	//Internet Explorerか?
	mstr = ua.match(/MSIE \d+/);
	if(mstr != null)
	{
		var vstr = mstr[0].match(/\d+/);
		if(parseInt(vstr[0]) >= 9) 
		{
			Sandbox.prototype.g_browsername = 'MSIE';
			//alert('major-version ' + vstr);
			return true;
		}		
	}
	//Firefoxか?
	mstr = ua.match(/Firefox\/\d+/);
	if(mstr != null){
		var vstr = mstr[0].match(/\d+/);
		if(parseInt(vstr[0]) >= 4) 
		{
			Sandbox.prototype.g_browsername = 'Firefox';
			//alert('major-version ' + vstr);
			return true;
		}		
	}
	//Operaか?
	if(ua.indexOf('Opera') > -1)
	{
		mstr = ua.match(/Version\/\d+/);
		if(mstr != null){
			var vstr = mstr[0].match(/\d+/);
			if(parseInt(vstr[0]) >= 11) 
			{
				Sandbox.prototype.g_browsername = 'Opera';
				//alert('major-version ' + vstr);
				return true;
			}		
		}		
	}
	
	return false;
}

base.js

window.addEventListener("load", init, false);
  
function init()
{
    var offsetX = 0,
    offsetY = 0,
    theCanvas,
    buttonDown,
    buttonMove,
    buttonUp,
    centerX = 0,
    centerY = 0,
    theContents,
    text,
    ctx,
    mouseX,
    mouseY,
    getRGB;
     
    theContents = document.getElementById("canvas");
    offsetX = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).width;
    offsetX = Number(offsetX.replace('px', ''));
    centerX = offsetX / 2;
  
    offsetY = (theContents.currentStyle || document.defaultView.getComputedStyle(theContents,'')).height;
    offsetY = Number(offsetY.replace('px', ''));
    centerY = offsetY / 2;

    
     
    //ボタンタイプ
    if(g_mobiledevice === true)
    {
        buttonDown = 'touchstart';
        buttonMove = 'touchmove';
        buttonUp = 'touchend';
    }
    else
    {
        buttonDown = 'mousedown';
        buttonMove = 'mousemove';
        buttonUp = 'mouseup';
    }
     
    document.getElementById('canvas').addEventListener('touchstart', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchmove', function() 
    {
        event.preventDefault();
    });
    document.getElementById('canvas').addEventListener('touchend', function() 
    {
        event.preventDefault();
    });
      
   document.getElementById('canvas').addEventListener(buttonUp, buttonDownHandler, false);
   document.getElementById('canvas').addEventListener(buttonMove, buttonDownHandler, false);
     
    animation();
    function animation()
    {
        draw();
        //requestAnimationFrame(animation);
    }
     
    function draw()
    {  
        ctx = document.getElementById('canvas').getContext("2d");
        //var element = document.getElementById("canvas");

        ctx.clearRect(0, 0, offsetX, offsetY);

        var w = 256;
        var j = 0;
        //console.log(w);

        for(var i = 0; i < w; i++)
        {
            ctx.beginPath();
            j = j + 1;
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + 255 + "," + j + "," + 0 + "," + 1 + ")"; //red
            ctx.fill();
        }

        w = 256 * 2;
        var s = 256;
        j = 256;
        for(var i = s; i < w; i++)
        {
            ctx.beginPath();
            j = j - 1;
            console.log(j);
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + j + "," + 255 + "," + 0 + "," + 1 + ")";
            ctx.fill();
        }

        w = 256 * 3;
        s = 256 * 2;
        j = 0;
        for(var i = s; i < w; i++)
        {
            ctx.beginPath();
            j = j + 1;
            console.log(j);
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + 0 + "," + 255 + "," + j + "," + 1 + ")";
            ctx.fill();
        }

        w = 256 * 4;
        s = 256 * 3;
        j = 256;
        for(var i = s; i < w; i++)
        {
            ctx.beginPath();
            j = j - 1;
            console.log(j);
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + 0 + "," + j + "," + 255 + "," + 1 + ")";
            ctx.fill();
        }

        w = 256 * 5;
        s = 256 * 4;
        j = 0;
        for(var i = s; i < w; i++)
        {
            ctx.beginPath();
            j = j + 1;
            console.log(j);
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + j + "," + 0 + "," + 255 + "," + 1 + ")";
            ctx.fill();
        }

        w = 256 * 6;
        s = 256 * 5;
        j = 256;
        for(var i = s; i < w; i++)
        {
            ctx.beginPath();
            j = j - 1;
            console.log(j);
            ctx.rect(i, 0, i + 1, offsetY);
            ctx.fillStyle = "rgba(" + 255 + "," + 0 + "," + j + "," + 1 + ")";
            ctx.fill();
        }
        
        ctx.beginPath();
        /* グラデーション領域をセット */
        var grad  = ctx.createLinearGradient(0,0, 0, offsetY);
        /* グラデーション終点のオフセットと色をセット */
        grad.addColorStop(0,'rgba(0, 0, 0, 0)');
        grad.addColorStop(1,'rgba(0, 0, 0, 1)');
        /* グラデーションをfillStyleプロパティにセット */
        ctx.fillStyle = grad;
        /* 矩形を描画 */
        ctx.rect(0, 0, offsetX, offsetY);
        ctx.fill();
        

        element = document.getElementById("canvas");

        element.style.webkitTransformOrigin = "left top";
        var s =  275 / offsetX;
        element.style.webkitTransform = "scaleX(" + s + ")";

        getRGB = document.getElementById("text");
        getRGB.innerHTML = "r: 0<br/> g: 0<br/>b: 0";
        
        
    }
     
    function buttonDownHandler(e)
    {
         setMouseXY(e);
         var imageData = ctx.getImageData(mouseX * offsetX / 275, mouseY, 1, 1);
         var pixel = imageData.data;
         
         //console.log(pixel[0] + ", " + pixel[1] + ", " + pixel[2]);
         getRGB.innerHTML = "r: " + pixel[0] + "<br/>g: " + pixel[1] + "<br/>b: " + pixel[2];
    }

    function setMouseXY(e)
    {
        if(g_mobiledevice)
        {
            var rect = event.currentTarget.getBoundingClientRect();
            mouseX = event.touches[0].pageX - rect.left;
            mouseY = event.touches[0].pageY - rect.top;
        }
        else
        if(g_browsername == 'MSIE' || g_browsername == 'Opera')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = event.clientX - rect.left;
            mouseY = event.clientY - rect.top;
        }
        else
        if(g_browsername == 'Firefox')
        {
            rect = document.getElementById("canvas").getBoundingClientRect();
            mouseX = e.clientX - rect.left;
            mouseY = e.clientY - rect.top;
        }
        else
        {
            rect = event.currentTarget.getBoundingClientRect();
            mouseX =  event.clientX - rect.left;
            mouseY =  event.clientY - rect.top;
        }
    }
}
 
// 各ブラウザ対応
window.requestAnimationFrame = (function()
{
    return window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    function(callback)
    {
        window.setTimeout(callback, 1000 / 60);
    };
}());

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

コメントフィード

トラックバック URL : http://www.htmlcode.jp/html5-javascript%e3%81%a7rgb%e5%80%a4%e3%82%92%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b/trackback/