如何使用 HTML5 Canvas 像素操作功能调整图像的对比度?我已经很容易成功地改变了图像的亮度,但对比度似乎有点复杂。
有谁知道我可以遵循的算法,或者我可以从中获取想法的示例 javascript 代码?
提前感谢您提供有关此问题的任何帮助。
如何使用 HTML5 Canvas 像素操作功能调整图像的对比度?我已经很容易成功地改变了图像的亮度,但对比度似乎有点复杂。
有谁知道我可以遵循的算法,或者我可以从中获取想法的示例 javascript 代码?
提前感谢您提供有关此问题的任何帮助。
既然你说你已经调整了亮度,听起来你只需要调整对比度的算法,这里有一个 C# 方法的线程(查看更新 2),代码的逻辑是合理的,并且可以在任何程序中工作:
我将发布所有过滤器的演示。您可以通过 id 或 class 在画布上轻松使用它。但问题是你不能用画布渲染它意味着你不保存或预览。因为画布不能渲染样式。
代码
<!-- canvas -->
<html>
<head>
<style>
img {
width: 33%;
height: auto;
float: left;
}
.origional {filter:none;}
.blur {-webkit-filter: blur(4px);filter: blur(4px);}
.brightness {-webkit-filter: brightness(0.30);filter: brightness(0.30);}
.contrast {-webkit-filter: contrast(180%);filter: contrast(180%);}
.grayscale {-webkit-filter: grayscale(100%);filter: grayscale(100%);}
.huerotate {-webkit-filter: hue-rotate(180deg);filter: hue-rotate(180deg);}
.invert {-webkit-filter: invert(100%);filter: invert(100%);}
.opacity {-webkit-filter: opacity(50%);filter: opacity(50%);}
.saturate {-webkit-filter: saturate(7); filter: saturate(7);}
.sepia {-webkit-filter: sepia(100%);filter: sepia(100%);}
.shadow {-webkit-filter: drop-shadow(8px 8px 10px green);filter: drop-shadow(8px 8px 10px green);}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script>
$( document ).ready(function() {
//origional canvas image
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("origional");
ctx.drawImage(img, 0, 0,300,300);
$('#button').click(function() {
canvas.classList.add("origional");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button1').click(function() {
canvas.classList.add("blur");
canvas.classList.remove("origional");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button2').click(function() {
canvas.classList.add("brightness");
canvas.classList.remove("blur");
canvas.classList.remove("origional");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button3').click(function() {
canvas.classList.add("contrast");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("origional");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button4').click(function() {
canvas.classList.add("grayscale");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("origional");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button5').click(function() {
canvas.classList.add("huerotate");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("origional");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button6').click(function() {
canvas.classList.add("invert");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("origional");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button7').click(function() {
canvas.classList.add("opacity");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("origional");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button8').click(function() {
canvas.classList.add("saturate");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("origional");
canvas.classList.remove("sepia");
canvas.classList.remove("shadow");
});
$('#button9').click(function() {
canvas.classList.add("sapia");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("origional");
canvas.classList.remove("shadow");
});
$('#button10').click(function() {
canvas.classList.add("shadow");
canvas.classList.remove("blur");
canvas.classList.remove("brightness");
canvas.classList.remove("contrast");
canvas.classList.remove("grayscale");
canvas.classList.remove("huerotate");
canvas.classList.remove("invert");
canvas.classList.remove("opacity");
canvas.classList.remove("saturate");
canvas.classList.remove("sepia");
canvas.classList.remove("origional");
});
});
</script>
</head>
<body>
<h1>
canvas
</h1>
<canvas id="canvas" width="300" height="300" ></canvas>
<br>
<div ><input type="button" id="button" value="origional" />
<input type="button" id="button1" value="blur" />
<input type="button" id="button2" value="brightness" />
<input type="button" id="button3" value="contrast" />
<input type="button" id="button4" value="grayscale" />
<input type="button" id="button5" value="huerotate" />
<input type="button" id="button6" value="invert" />
<input type="button" id="button7" value="opacity" />
<input type="button" id="button8" value="saturate" />
<input type="button" id="button9" value="sepia" />
<input type="button" id="button10" value="shadow" />
</div>
<br>
<!-- image -->
<h1>
image
</h1>
<img id="origional" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img id="blur" class="blur" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="brightness" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="contrast" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="grayscale" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="huerotate" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="invert" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="opacity" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="saturate" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="sepia" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
<img class="shadow" src="https://lh5.googleusercontent.com/-bSF6HLBpWZU/AAAAAAAAAAI/AAAAAAAAABI/6UVYOKY6QhE/photo.jpg" alt="Pineapple" width="300" height="300">
</body>
</html>
这里现场演示
查看Pixastic提供了大量出色的效果,您可以使用示例编辑器来查看其中的一些功能。Pixastic 亮度/对比度示例
这是我制作的对比鲜明的马里奥的演示。
如果您不想使用整个库,还可以查看对比方法的 pixastic github 源代码,了解它如何实现到您的代码中。