Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
c#
Is it possible to send operators such as '<' or '>' in method parameters?

Public void change_color(Operator lessThan, Operator lessThan ){

if(img.GetPixel(x, y).R lessThan 0 && img.GetPixel(x, y).G MoreThan 128 && img.GetPixel(x, y).B LessThan 110)
{
//do something
}

}
Posted
Comments
ZurdoDev 18-Jan-16 15:46pm    
I doubt it. You could of course send your own enumeration through and then use if statements or something along those lines.
Sergey Alexandrovich Kryukov 18-Jan-16 16:11pm    
Of course it's possible to abstract out the operators, only not in this syntax. Please see Solution 2.
—SA
BillWoodruff 18-Jan-16 20:18pm    
Ryan, this is what Delegates, and their newer incarnations in 'Action and 'Func, are designed for.
BillWoodruff 18-Jan-16 20:43pm    
What is your current level of understanding, and using, Delegates ? Have you ever used the 'Action or 'Func Delegate forms ?

Not the way you're sample is thinking, no.

You'd be better off just creating a second method with the appropriate operators in place.

Other methods are possible but they introduce performance hits on top of the bad code you're already writing.

Get/SetPixel are VERY slow to execute once and you're getting the exact same pixel three times instead of once. Putting an interpreted operator into the mix and you've got the slowest possible code you could write to do this.

Search the articles here for "Image processing for dummies[^]" and you'll come up with a bunch of articles on how to do pixel work without using GetPixel and it'll be extremely fast compared to what you're writing.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-16 16:12pm    
I still think that the inquirer deserves the explanation on how to abstract out the operators. This is quite possible. Please see Solution 2.
—SA
Dave Kreskowiak 18-Jan-16 16:20pm    
True, but given the code he has written so far, the method to do this is WAY over his head. That's why I left it out.
Sergey Alexandrovich Kryukov 18-Jan-16 17:01pm    
Maybe you right, but I tried. The question makes certain sense, demonstrates that the inquirer can think. Anyone should be given a chance. :-)
—SA
BillWoodruff 18-Jan-16 20:24pm    
+5 Wise advice; while I think it's a very good thing for the OP to learn about Delegate, Action, and Func, for working with Pixels with any performance, a function that works on a single Pixel does not make sense.
You need to understand that an operator can be considered as syntactic sugar over the concept of method. For example, you are talking about binary operators; functionally, they are equivalent to a function accepting two arguments (each representing an operant) and returning the value. In your case, they return Boolean. Therefore, your arguments should be delegate instances. Say, for int operands, the delegate type is delegate bool Comparison(int left, int right).

But you don't need to do even that. You already have such declaration as generic type System.Func<int, int, bool>. Moreover, you can make your own class generic and use this generic delegate; this way, you can abstract from the types of your operands to be compared. First, let's consider the method of your non-generic class. It could be something like
C#
using System;

// ...

public void ChangeColor(
        Func<int, int, bool> lessThen,
        Func<int, int, bool> greaterOrEqualThen /* ... */) {
   // ...
   if (lessThen != null) {
        if (lessThen(myPixel, 0))
            DoDomething();
        else
            DoSomethingElse();
    } else
        DoSomethingByDefault();                
   // ...
}

// ...

// Here is how you can call it:
ChangeColor(
   new Func<int, int, bool>((a, b) => { return a < b; }),
   new Func<int, int, bool>((a, b) => { return a >= b; }));

I understand that this syntax of call is not as simple as you wanted, but this is what you have.

To to it in generic way, you can have something like
C#
using System;

// ...

class GraphicProcessing<OPERAND> {
    public void ChangeColor(
            Func<OPERAND, OPERAND, bool> lessThen,
            Func<OPERAND, OPERAND, bool> greaterOrEqualThen /* ... */) {
        OPERAND myPixel = //...
        OPERAND compareTo = //...
        // ...
        if (lessThen != null) {
            if (lessThen(myPixel, compareTo))
                DoDomething();
            else
                DoSomethingElse();
        } else
            DoSomethingByDefault();                
        // ...
    }
}


Also note that you are trying to use very slow API, GetPixel, prohibitively slow. If you mean to use System.Drawing, such things can be done efficiently using System.Drawing.Bitmap.LockBits:
Bitmap.LockBits Method (System.Drawing)[^].

You will find a very informative code sample on the MDSN help page on one of the LockBits methods referenced above.

—SA
 
Share this answer
 
v6
Comments
BillWoodruff 18-Jan-16 20:21pm    
+5 Even though I think Dave's practical advice to avoid this is wise, here, if I had to do this, I would definitely use Funcs. I'm tempted to post an alternative write of the code here.
Sergey Alexandrovich Kryukov 18-Jan-16 21:04pm    
I have to agree, most likely, in the particular context given by the inquirer, it makes more practical sense. I just think that the inquirer has a sound idea of abstracting out the operators, and it deserves some answer. Besides, it's just useful to understand such things.
Thank you.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900