Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Tip/Trick

Simple Method to Invert a Color

Rate me:
Please Sign up or sign in to vote.
4.53/5 (5 votes)
21 Feb 2015CPOL 16.8K   3   3
A method to get the inverted equivalent of a specified color.

Introduction

This method accepts a System.Drawing.Color object and uses the RGB values to create and return an object representing the color's inversion.

public static Color Invert (this Color color)
{
    return Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
}

In this example the method is static and is an extension method so you can call it like so:

Color redInverted = Color.Red.Invert();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer JBHworks
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugProblem when your color is 127,127,127 Pin
SBGTrading24-Feb-15 1:30
SBGTrading24-Feb-15 1:30 
SuggestionCould be faster Pin
D. Christian Ohle22-Feb-15 5:47
D. Christian Ohle22-Feb-15 5:47 
GeneralMy vote of 5 Pin
webmaster44222-Feb-15 4:52
webmaster44222-Feb-15 4:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.