p5使用十六进制字符串和alpha设置填充颜色 [英] p5 set fill color using hex string and alpha

查看:160
本文介绍了p5使用十六进制字符串和alpha设置填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对p5.js中的颜色使用十六进制值,但是在使用它并同时使用alpha时遇到了麻烦.我想用一个变量设置颜色,用另一个变量设置alpha.

I'm trying to use hexadecimal values for colors in p5.js and I'm having trouble using it and using an alpha at the same time. I'd like to set the color with one variable and the alpha with another.

let myColor = '#FF0000';
let myAlpha = 128;
function setup() {
  createCanvas(200,200);
}
function draw() {
  fill(color(myColor), myAlpha);
  noStroke();
  ellipse(100, 100, 50, 50);
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
<html>
  <head></head>
  <body></body>
</html>

这适用于颜色,但是alpha最终为255(100%).

This works for the color, but the alpha ends up being 255 (100%).

推荐答案

setAlpha()记录在案在p5.Color 上,并采用0到255之间的值.

setAlpha() is documented on p5.Color and takes values from 0 to 255.

输入您的代码

let c = color(myColor);
c.setAlpha(myAlpha);
fill(c);

function setup(){
  createCanvas(300,300);
}

var alphaValue = 50;

function draw(){
  var c = color('#FF0000');
  fill(c);
  rect(100,100,100,100);
  c = color('#0000FF');
  c.setAlpha(alphaValue);
  fill(c);
  rect(150,150,100,100);
}

function mouseClicked(){
    if (alphaValue > 50){
	    alphaValue = 50;
	} else {
	    alphaValue = 255;
	}
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

这篇关于p5使用十六进制字符串和alpha设置填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆