0

EDIT: Solved! I just multiplied my outcomes with -1, this solved the problem

I'm trying to convert doubles ranging from -1.0 to 1 to integers ranging from 0 to 255. But when I try to do this, it returnes negative values. I've tried using the code from people that have asked the same question in here, but I cannot seem to get it to work like it should.

The code that generates the double:

 temp = perlinMap.GetValue((double)(x / this.samples), (double)(y / this.samples), 1d);

 if (temp <= 0.1d && temp >= -1.0d)
 {
    colorArray[(int)x, (int)y] = toGradient(temp, -1.0d, 0.1d, 0.0d, 0.0d, 104d, 153d, 173d, 255d);
 }

The method:

 Color toGradient(double value, double minValue, double maxValue, double rMin, double rMax, double gMin, double gMax, double bMin, double bMax)
 {
    r = (((value - minValue) * (rMax - rMin)) / (maxValue - minValue)) + rMin;
    g = (((value - minValue) * (gMax - rMin)) / (maxValue - minValue)) + gMin;
    b = (((value - minValue) * (bMax - rMin)) / (maxValue - minValue)) + bMin;

    color = Color.FromArgb(255, (int)r, (int)g, (int)b);
        return color;
 }

Same method, different approach. Returns the same, negative, numbers

 Color toGradient(double value, double minValue, double maxValue, double rMin, double rMax, double gMin, double gMax, double bMin, double bMax)
 {

    r = (double)(rMin + ((value - minValue) * (rMax - bMin) / (maxValue - minValue)));
    g = (double)(gMin + ((value - minValue) * (gMax - bMin) / (maxValue - minValue)));
    b = (double)(bMin + ((value - minValue) * (bMax - bMin) / (maxValue - minValue)));

    color = Color.FromArgb(255, (int)r, (int)g, (int)b);
    return color;
  }
user3376217
  • 35
  • 3
  • 9

1 Answers1

0

I think the first thing you should do before using this formula is to use a translation. In order to remove all negatives values from your values between -1 an 1 you should first add 1 to all these elements, then apply your min-max transformation. I think that will get the job done

Franck Ngako
  • 163
  • 6