您可以使用下面的代碼來完成它。它使用畫布來處理圖像。該概念基於使用原始圖像的灰度版本檢測圖像是黑暗還是光線。
這是根據div的背景顏色工作version。 如果你想使用圖像作爲背景,請參考我的代碼here。我已經上傳了一張帶有背景圖片的樣本。由於交叉來源問題,我無法制作小提琴或在線演示。
簡單RGB背景的代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic text color demo using canvas</title>
</head>
<body>
<label><b>Image with text</b></label>
<div id="content" style="box-shadow:0px 0px 1px #000;width:200px;height:100px;background-color:#333;">
<b style="z-index:2;position:absolute;color:black !important;">BLACK Text</b>
<br><b style="z-index:2;position:absolute;color:white !important;">WHITE Text</b>
<br><b style="z-index:2;position:absolute;color:rgb(127,127,127) !important;">GRAY Text</b>
<br><b style="z-index:2;position:absolute">CORRECTED COLOR</b>
</div><br><br>
<label><b>Variables</b></label>
<div id="height">
</div>
<div id="width">
</div>
<div id="dataLength">
</div>
<div id="blackCount">
</div>
<br>
<label><b>Canvas for grayscale</b></label><br>
<canvas id="myCanvas" width="200px" height="100px" style="box-shadow:0px 0px 1px #000"></canvas>
<script>
/* ~~~~~ This function loops over various images ~~~~~ */
var c=0;
var color;
function loop(){
c++;
document.getElementById('content').style.backgroundColor='rgb('+(c*29)%255+','+(c*29)%255+','+(c*29)%255+')';
var imageObj = new Image();
imageObj.onload = function() {
drawImage(color);
}
color=document.getElementById('content').style.backgroundColor;
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
var x = 0;
var y = 0;
context.rect(0,0,canvas.width, canvas.height);
context.fillStyle='rgb('+(c*29)%255+','+(c*29)%255+','+(c*29)%255+')';
context.fill();
/* ~~~~~ Print height and widht of image ~~~~~ */
document.getElementById('height').innerHTML="height: "+imageObj.height;
document.getElementById('width').innerHTML="width: "+imageObj.width;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
var imageData = context.getImageData(x, y, canvas.width, canvas.height);
var data = imageData.data;
/* ~~~~~ This part converts the image to grayscale ~~~~~ */
for(var i = 0; i < data.length; i += 4) {
var brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
// red
data[i] = brightness;
// green
data[i + 1] = brightness;
// blue
data[i + 2] = brightness;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~ This part counts the number of dark pixels ~~~~~ */
var count=0;
for(var j=0;j<data.length;j+=4){
if(data[j]<200 && data[j+1]<200 && data[j+2]<200){
count+=4;
}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~ If the number of dark pixels are 80% of total pixels, then assume image to be dark ~~~~~ */
if(count>=(data.length*0.8)){
document.getElementById('content').style.color="white";
}else{
document.getElementById('content').style.color="black";
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~ Print total pixel and counted dark pixels ~~~~~ */
document.getElementById('dataLength').innerHTML ="Data length"+data.length+" Data length/2 "+(data.length/2);
document.getElementById('blackCount').innerHTML ="Black Count"+count;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// Draw grayscale image on cavas
context.putImageData(imageData, x, y);
}
setInterval(loop,2000);
</script>
</body>
</html>