gpt4 book ai didi

c# - 如何将元素的背景颜色与 2 个半透明元素背景颜色相匹配

转载 作者:行者123 更新时间:2023-11-29 15:37:45 24 4
gpt4 key购买 nike

我做了一个jsfiddle

在 fiddle 中有 3 个 div。

  1. 左边的是紫色
  2. 右边的是蓝色
  3. 蓝色 div 上面的那个有一种未知颜色

第三个 div(位于右侧蓝色 div 之上的那个)所需的颜色是左侧 div(紫色那个)的颜色。

问题是所有 div 的不透明度为 0.7,并且必须保持不变(以便看到它们后面的内容)。

那么我的问题是,如何计算左侧紫色和右侧蓝色之间的差异,以便我可以给第三个 div 一种颜色,使其与紫色 div 的颜色相匹配当它覆盖右侧的蓝色 div 时在左侧。

我不是要你用眼睛去猜,因为我有很多其他颜色可以用来做这个,我只是想知道是否有一个计算允许我使用 JQuery 或 Javascript 来做这个?

到目前为止,我还没有任何 JS 代码,因为我什至不知道从哪里开始。


HTML

<div id="target" class="opacity purple"></div>
<div id="actualBackground" class="opacity blue"></div>
<div id="sameColourAsTarget" class="opacity purple2"></div>

CSS

div{
position:absolute;
}
#target{
left:0px;
top:0px;
height:150px;
width:150px;
}
#actualBackground{
left:150px;
top:0px;
height:150px;
width:150px;
}
#sameColourAsTarget{
left:150px;
top:50px;
height:50px;
width:50px;
}
.opacity{
opacity:0.7;
}
.purple{
background-color:rgb(152, 3, 214);
}
.blue{
background-color:rgb(10, 127, 188);
}
.purple2{
background-color:rgb(175, 3, 150);
}



非常感谢任何帮助,

谢谢

最佳答案

检查 fiddle here用于纯 Javascript 解决方案。

/**
* Gets the mixed color obtained by overlaying a front color
* with the specified opacity over a solid back color.
*/
function mix(back, front, opacity) {
var mixed = {
r: (1 - opacity) * back.r + opacity * front.r,
g: (1 - opacity) * back.g + opacity * front.g,
b: (1 - opacity) * back.b + opacity * front.b
}

return mixed;
}

/**
* Gets the front color that can be overlaid with the specified
* opacity over a solid back color to get the same color as the
* mixed color.
*/
function unmix(mixed, back, opacity) {
var front = {
r: (mixed.r - back.r) / opacity + back.r,
g: (mixed.g - back.g) / opacity + back.g,
b: (mixed.b - back.b) / opacity + back.b
}

return front;
}

/**
* Converts an rgb string to a color object in the following form
* {r: 255, g: 255, b: 255}
*/
function rgbToColor(rgb) {
var matches = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);

return {
r: parseInt(matches[1]),
g: parseInt(matches[2]),
b: parseInt(matches[3])
}
}

/**
* Converts a color object of from {r: 255, g: 255, b: 255} to
* an rgb string
*/
function colorToRgb(color) {
var r = Math.round(color.r);
var g = Math.round(color.g);
var b = Math.round(color.b);

return "rgb(" + r + "," + g + "," + b + ")";
}

// get properties of the back element
var backEl = document.getElementById("actualBackground");
var backStyle = window.getComputedStyle(backEl);
var backColor = backStyle.backgroundColor;
var backOpacity = backStyle.opacity;

// get properties of the target element
var mixedEl = document.getElementById("target");
var mixedStyle = window.getComputedStyle(mixedEl);
var mixedColor = mixedStyle.backgroundColor;
var mixedOpacity = mixedStyle.opacity;

// calculate the actual back color by mixing the back element's
// properties with solid white
var back = mix({
r: 255,
g: 255,
b: 255
}, rgbToColor(backColor), backOpacity);

// calculate the actual target color by mixing the target element's
// properties with solid white
var mixed = mix({
r: 255,
g: 255,
b: 255
}, rgbToColor(mixedColor), mixedOpacity);

// calculate the overlay's color by unmixing the back and the target
// colors with an opacity of 0.7 (could also be retrieved from the overlay element
var front = unmix(mixed, back, 0.7);

// get the overlay's element and apply the calculated color
var frontEl = document.getElementById("sameColourAsTarget");
frontEl.style.backgroundColor = colorToRgb(front);
frontEl.style.opacity = 0.7;

关于c# - 如何将元素的背景颜色与 2 个半透明元素背景颜色相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com