Python to C# Conversion of RGBA values not working

Posted by clangers on Stack Overflow See other posts from Stack Overflow or by clangers
Published on 2012-11-24T22:40:44Z Indexed on 2012/11/24 23:03 UTC
Read the original article Hit count: 153

Filed under:
|
|

I'm currently converting some python code to C#, and I'm having an issue with the RGBA C# libraries

# Original Python Code:
    d = math.sqrt( (x - size/2.0)**2 + (y - size/2.0)**2 )
    rgbVal = int(200*d/md + 50)
    rgba = (0,0,0, 255 - rgbVal)
    img.putpixel((x,y), rgba)

// My C# Code
double d = Math.Sqrt(Math.Pow((x - DotSize / 2.0), 2) + Math.Pow((y - DotSize / 2.0), 2));
int rgbVal = (int) (200 * d / md + 50);
Color color = Color.FromArgb(255 - rgbVal, 0, 0, 0);     // ** ERROR **
img.SetPixel(x,y, color);

At both instances of the code d is equal to 106 and md is equal to 53. However the resulting rgbVal value is 450. This would obviously mean that 255 - 450 is -195, which causes an error to be thrown as each individual value must be between 0 and 255. Anyone have any idea how I can fix this. Please note that the data is the same when running both the python and C# versions.

© Stack Overflow or respective owner

Related posts about c#

Related posts about python