计算 WPF 中的选择画笔颜色
我注意到,当将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
跟进 HB 的答案,
我过去用以下公式计算了不透明度
,该公式反转为
TextBox
默认的Background
设置为白色,SelectionOpacity
设置为 0.4。正如 HB 所解释的,您无法使用这些默认设置获得颜色,因为红色值将显示为 -130。这给你提供了 3 个选项,更改
Background
、更改SelectionOpacity
或不执行此操作:)更改
TextBox 背景
可能不是问题如果您想要保留选项 2,请更改SelectionOpacity
我们不希望红色低于 0,因此
结果
为 0,604,因此您可以使用此
SelectionOpacity
值计算出应用不透明度后,SelectionBrush
需要设置为#0056B3
才能变为#6599D1
。以下是
TextBox
使用这些值的外观Following up on the answer by H.B.
I've calculated opacity with the following formula in the past
which inverts to
The
TextBox
has defaultBackground
set to White andSelectionOpacity
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
, changeSelectionOpacity
or don't do it :)Changing the
TextBox Background
probably isn't something you wanna do so that leaves option 2, changeSelectionOpacity
We don't want Red to go below 0 so
which gives
This results in 0,604 so with this
SelectionOpacity
value you can calculate that theSelectionBrush
needs to be set to#0056B3
to become#6599D1
once the Opacity has been applied.Here's how the
TextBox
look with these values好问题,但我认为这是不可能的。如果 ControlTemplate 内有一些覆盖,则无法制定一个函数来计算较暗的颜色,然后该颜色最终将成为预期的颜色。
例如,当您输入红色时,即:
255,0,0
,您将得到255,153,153
,现在需要应用于初始颜色的函数需要使红色变暗,这当然只能在红色通道中完成,因为绿色和蓝色已经为零。然而,问题不在于红色通道(最终为 255,因此不受影响),因此对其进行任何更改只会进一步降低颜色饱和度,而不是使其变暗。编辑:为了使其更数学化,选择的透明度应用的函数是:
如果将其应用于颜色的所有通道,您会发现情况确实如此。现在我们如何获得我们想要的值呢?很简单,我们反转函数,就是这样:
太棒了!现在让我们将其应用到您的颜色上:
嗯,我想毕竟不太好。
这个负值正是为什么这并不总是可能的原因,每种颜色的成分低于
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 get255,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:
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:
Great! Now lets apply that to your colour:
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.