473,513 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

color inversion

sam
Hi
Is there a method, or has anybody already ready written one to determine a
contrasting color to a background color for text. In my case during a paint
of a cell in a data grid.
Thanks
Nov 21 '05 #1
4 2399
this gives the opposite of a color
TextBox1.ForeColor = Color.FromArgb(255 - TextBox1.BackColor.R, 255 -
TextBox1.BackColor.G, 255 - TextBox1.BackColor.B)

hth Peter

"sam" <sa*@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Hi
Is there a method, or has anybody already ready written one to determine a contrasting color to a background color for text. In my case during a paint of a cell in a data grid.
Thanks

Nov 21 '05 #2
Peter Proost wrote:
this gives the opposite of a color
TextBox1.ForeColor = Color.FromArgb(255 - TextBox1.BackColor.R, 255 -
TextBox1.BackColor.G, 255 - TextBox1.BackColor.B)

hth Peter

"sam" <sa*@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Hi
Is there a method, or has anybody already ready written one to determine


a
contrasting color to a background color for text. In my case during a


paint
of a cell in a data grid.
Thanks



But that doesn't always give a contrasting colour as the OP wants. If
the color is close to 50% grey, the resulting colour with your method
will also be close to 50% grey.

Just off the top of my head, you could look at the greyvalue of the
color (20% red, 60% green, 20% blue) and then see if it's below 50%
grey, use white (or a very bright color) and if it's above 50% grey, use
black (or a dark color).

i.e.

dim nGrey,

nGrey = 0.2 * TextBox1.BackColor.R + 0.6 * TextBox1.BackColor.G + 0.2 *
TextBox1.BackColor.B

if nGrey>128 then
Texbox1.ForeColor(color.black)
Else
Texbox1.ForeColor(color.white)
Endif

btw those percentages come from the graphics world to convert color intp
greyscale, I didn't make them up.

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #3
you're right, it inverts the color it doesn't give the biggest contrast, I
think that in bobpowell's faq there's an example of what the op wants but
I'm not 100% sure

Peter

"C-Services Holland b.v." <cs*@REMOVEcsh4u.nl> wrote in message
news:7-********************@zeelandnet.nl...
Peter Proost wrote:
this gives the opposite of a color
TextBox1.ForeColor = Color.FromArgb(255 - TextBox1.BackColor.R, 255 -
TextBox1.BackColor.G, 255 - TextBox1.BackColor.B)

hth Peter

"sam" <sa*@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Hi
Is there a method, or has anybody already ready written one to
determine
a
contrasting color to a background color for text. In my case during a


paint
of a cell in a data grid.
Thanks



But that doesn't always give a contrasting colour as the OP wants. If
the color is close to 50% grey, the resulting colour with your method
will also be close to 50% grey.

Just off the top of my head, you could look at the greyvalue of the
color (20% red, 60% green, 20% blue) and then see if it's below 50%
grey, use white (or a very bright color) and if it's above 50% grey, use
black (or a dark color).

i.e.

dim nGrey,

nGrey = 0.2 * TextBox1.BackColor.R + 0.6 * TextBox1.BackColor.G + 0.2 *
TextBox1.BackColor.B

if nGrey>128 then
Texbox1.ForeColor(color.black)
Else
Texbox1.ForeColor(color.white)
Endif

btw those percentages come from the graphics world to convert color intp
greyscale, I didn't make them up.

--
Rinze van Huizen
C-Services Holland b.v.

Nov 21 '05 #4
I don't believe there is any reliable method for what you want.
Heres a selection of methods (in VB) each of which has their own problems.

Private Function InvertColor(ByVal ColorIn As Color) As Color
Return Color.FromArgb(ColorIn.ToArgb Xor &HFFFFFF)
End Function

Private Function ContrastingColor(ByVal ColorIn As Color) As Color
If MidRange(ColorIn) Then Return Color.White
Return InvertColor(ColorIn)
End Function

Private Function MidRange(ByVal ColorIn As Color) As Boolean
Return ColorIn.R > 100 AndAlso ColorIn.R < 160 AndAlso _
ColorIn.G > 100 AndAlso ColorIn.G < 160 AndAlso _
ColorIn.B > 100 AndAlso ColorIn.B < 160
End Function

Private Function BlackOrWhite(ByVal colorin As Color) As Color
If colorin.GetBrightness < 0.5 Then
Return Color.White
End If
Return Color.Black
End Function

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"sam" <sa*@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Hi
Is there a method, or has anybody already ready written one to determine
a
contrasting color to a background color for text. In my case during a
paint
of a cell in a data grid.
Thanks

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2405
by: Thomas Matthews | last post by:
Hi, According to Robert Martin's Dependency Inversion Principle, http://www.objectmentor.com/resources/articles/dip.pdf, when there is a need to test the type of an object, the code inside the...
2
2607
by: Tanari | last post by:
I'm interested in learning about using Context Inversion of Control in c++, but all of the examples I can find use C# or Java. Can someone direct me to an example of Context IoC written in C++? ...
14
2966
by: pamelafluente | last post by:
Hi guys, I have a hard problem :) I have the following situation: <span onmouseover = "SelectionEffect()"> <div style="width: 50px; height: 50px; background-color:#FFE4E1"></div> </span>
59
6871
by: pamelafluente | last post by:
Hi, If I have: <div style="background:anyColorHere"Hi </div> where anyColorHere is any hex string that represent a valid color. How can I invert that background color "anyColorHere" ? Can...
3
427
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
2
2112
by: sweetchuck74 | last post by:
Nowadays, DI (Dependency Inversion) is one of the widely used design pattern. It provides several benefits, such as (1) loose coupling between component (2) effective and easy testing (component...
3
4960
by: PeteJ | last post by:
Hello all.. I wrote code in C to invert n x n matrices, where 1<n<39 and the matrices are guaranteed to be invertible. Prior to this task, I haven't really seen linear algebra since my sophmore...
5
2081
by: jayagowri | last post by:
Can anyone explain me the concept of inversion of control in spring pls
3
1993
by: Danno | last post by:
Just that simple question....out of curiosity.
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.