473,587 Members | 2,580 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 2405
this gives the opposite of a color
TextBox1.ForeCo lor = Color.FromArgb( 255 - TextBox1.BackCo lor.R, 255 -
TextBox1.BackCo lor.G, 255 - TextBox1.BackCo lor.B)

hth Peter

"sam" <sa*@discussion s.microsoft.com > wrote in message
news:08******** *************** ***********@mic rosoft.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.ForeCo lor = Color.FromArgb( 255 - TextBox1.BackCo lor.R, 255 -
TextBox1.BackCo lor.G, 255 - TextBox1.BackCo lor.B)

hth Peter

"sam" <sa*@discussion s.microsoft.com > wrote in message
news:08******** *************** ***********@mic rosoft.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.BackCo lor.R + 0.6 * TextBox1.BackCo lor.G + 0.2 *
TextBox1.BackCo lor.B

if nGrey>128 then
Texbox1.ForeCol or(color.black)
Else
Texbox1.ForeCol or(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*@REMOVEcsh4 u.nl> wrote in message
news:7-*************** *****@zeelandne t.nl...
Peter Proost wrote:
this gives the opposite of a color
TextBox1.ForeCo lor = Color.FromArgb( 255 - TextBox1.BackCo lor.R, 255 -
TextBox1.BackCo lor.G, 255 - TextBox1.BackCo lor.B)

hth Peter

"sam" <sa*@discussion s.microsoft.com > wrote in message
news:08******** *************** ***********@mic rosoft.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.BackCo lor.R + 0.6 * TextBox1.BackCo lor.G + 0.2 *
TextBox1.BackCo lor.B

if nGrey>128 then
Texbox1.ForeCol or(color.black)
Else
Texbox1.ForeCol or(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(ByV al ColorIn As Color) As Color
Return Color.FromArgb( ColorIn.ToArgb Xor &HFFFFFF)
End Function

Private Function ContrastingColo r(ByVal ColorIn As Color) As Color
If MidRange(ColorI n) Then Return Color.White
Return InvertColor(Col orIn)
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(By Val colorin As Color) As Color
If colorin.GetBrig htness < 0.5 Then
Return Color.White
End If
Return Color.Black
End Function

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"sam" <sa*@discussion s.microsoft.com > wrote in message
news:08******** *************** ***********@mic rosoft.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
2422
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 "switch cases" should be placed into the parent class. However, I am finding that this conflicts with the other principles -- the objects now...
2
2619
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++? Thank-you. http://www.gotw.ca/resources/clcm.htm for info about ]
14
2980
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
6913
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 you point (or suggest) a script
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 operations, mostly matrix inversion and trasposition. The dimentions of the matrices used are about 29x19,19x19 and 29x29. During the calculation, I...
2
2124
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 testing or unit testing) What are the drawbacks or disadvantages of using DI? Thank you very much for your answer in advance.
3
4968
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 year of undergrad, but I believe my algorithm implements Gaussian elimination. I append an identity matrix to the right of the one I wish to invert, use...
5
2089
by: jayagowri | last post by:
Can anyone explain me the concept of inversion of control in spring pls
3
1997
by: Danno | last post by:
Just that simple question....out of curiosity.
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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 we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.