计算 WPF 中的选择画笔颜色

发布于 2024-11-29 15:00:26 字数 211 浏览 3 评论 0原文

我注意到,当将 wpf 中的文本框选择设置为红色,并使用颜色选择器验证颜色时,颜色褪色为#FF9999。

我有客户要求的选择画笔的特定颜色。有没有办法计算我应该将 SelectionBrush 设置为什么颜色,以便在选择文本时显示确切的颜色?

目前我需要将选择画笔设置为#6599D1,但设置后,颜色为#C1D6ED。

如何计算起始颜色以便最终颜色是我需要的?

I have noticed when setting the selection of a text box in wpf to red, and using a color picker to verify the color, the color faded to #FF9999.

I have a specific color my client requires for the selection brush. Is there a way to calculate what color i should set the SelectionBrush to so when the text is selected, the exact color is displayed?

Currently I need to have the selection brush as #6599D1, but once set, the color is #C1D6ED.

How can I calculate the starting color so the end color is what I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有你我更好 2024-12-06 15:00:26

跟进 HB 的答案,

我过去用以下公式计算了不透明度

Red = OriginalRed * Opacity + (1-Opacity) * BackgroundRed

,该公式反转为

(Red - (1-Opacity) * BackgroundRed) / Opacity = OriginalRed

TextBox 默认的 Background 设置为白色,SelectionOpacity 设置为 0.4。
正如 HB 所解释的,您无法使用这些默认设置获得颜色,因为红色值将显示为 -130。这给你提供了 3 个选项,更改 Background、更改 SelectionOpacity 或不执行此操作:)

更改 TextBox 背景 可能不是问题如果您想要保留选项 2,请更改 SelectionOpacity

我们不希望红色低于 0,因此

(101 - (1-Opacity) * 255) = 0

结果

1 - (101/255) = Opacity

为 0,604,因此您可以使用此 SelectionOpacity 值计算出应用不透明度后,SelectionBrush 需要设置为 #0056B3 才能变为 #6599D1

以下是 TextBox 使用这些值的外观

<TextBox SelectionBrush="#0056B3"
         SelectionOpacity="0.604" />

在此处输入图像描述

Following up on the answer by H.B.

I've calculated opacity with the following formula in the past

Red = OriginalRed * Opacity + (1-Opacity) * BackgroundRed

which inverts to

(Red - (1-Opacity) * BackgroundRed) / Opacity = OriginalRed

The TextBox has default Background set to White and SelectionOpacity set to 0.4.
As H.B. explained, you can't achieve your color with these default settings since the Red value will come out as -130. This leaves you with 3 options, Change Background, change SelectionOpacity or don't do it :)

Changing the TextBox Background probably isn't something you wanna do so that leaves option 2, change SelectionOpacity

We don't want Red to go below 0 so

(101 - (1-Opacity) * 255) = 0

which gives

1 - (101/255) = Opacity

This results in 0,604 so with this SelectionOpacity value you can calculate that the SelectionBrush needs to be set to #0056B3 to become #6599D1 once the Opacity has been applied.

Here's how the TextBox look with these values

<TextBox SelectionBrush="#0056B3"
         SelectionOpacity="0.604" />

enter image description here

尹雨沫 2024-12-06 15:00:26

好问题,但我认为这是不可能的。如果 ControlTemplate 内有一些覆盖,则无法制定一个函数来计算较暗的颜色,然后该颜色最终将成为预期的颜色。

例如,当您输入红色时,即:255,0,0,您将得到255,153,153,现在需要应用于初始颜色的函数需要使红色变暗,这当然只能在红色通道中完成,因为绿色和蓝色已经为零。然而,问题不在于红色通道(最终为 255,因此不受影响),因此对其进行任何更改只会进一步降低颜色饱和度,而不是使其变暗。

编辑:为了使其更数学化,选择的透明度应用的函数是:

f(x) = 0.4x + 153

如果将其应用于颜色的所有通道,您会发现情况确实如此。现在我们如何获得我们想要的值呢?很简单,我们反转函数,就是这样:

f^(-1)(x) = -2.5(153.0 - x)

太棒了!现在让我们将其应用到您的颜色上:

R:-130.0
重力:0
乙:140

嗯,我想毕竟不太好。

这个负值正是为什么这并不总是可能的原因,每种颜色的成分低于 153 都是不可逆的。

Good question but i think this is impossible. If there is some overlay inside the ControlTemplate you cannot formulate a function which calculates a darker colour which then will end up as the intended colour.

e.g. when you input red which is: 255,0,0 you get 255,153,153, now the function that would need to be applied to your initial colour would need to darken the Red, this of course could only be done in the red channel as green and blue are already zero. The problem is not the red channel however (which ends up as 255 and hence is not affected), so any changes to that will only serve to desaturate the colour even more instead of darkening it.

Edit: To make this a bit more mathmatical, the function that is applied by the transparency of the selection is:

f(x) = 0.4x + 153

If you apply this to all the channels of your colour you will see that this is indeed the case. Now how do we get the values we want? Quite simple, we invert the function, which is this:

f^(-1)(x) = -2.5(153.0 - x)

Great! Now lets apply that to your colour:

R: -130.0
G: 0
B: 140

Hmm, not so great after all i suppose.

This negative value is exactly why this is not always possible, every colour which has components below 153 is not reversible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文