Questions Regarding Function Usage in gvpr

I am currently trying to use gvpr and have some questions, primarily related to functions.

Does gvpr perform automatic type conversion when the parameter types do not match during function calls? I defined a function of type string xxx(float) and called it with xxx(16), but the variable inside the function received a value of 0. xxx(16.0) is right.

Can gvpr implement functions that return arrays or accept array parameters, similar to int[] xxx(float) or void xxx(float, int[])? I am working on an HSV to RGB conversion function and would like to return the RGB components separately. However, it appears that I can only concatenate the RGB components into a single integer using bitwise operations and return that as the result.

good questions, definitive answers require delving into the *gvpr source. Here are some non-definative answers:

  • functions are a gvpr weakness
  • type conversion is not always performed correctly - when in doubt, cast explicitly
    (e.g. xyz=(int) myfunct(…) )
  • seemingly, functions can not return arrays.
  • nor can functions accept arrays as arguments
  • Here is a suggestion. Call it three times, once for each R, G, or B - like so:
BEGIN{
  float h, s, v;
  int r,g,b;

  int bogus_hsvtorgb(float H, float S, float V, string which) {
    int R, G, B, Val;
    R=V*255;  // BOGUS
    G=S*255;  // BOGUS
    B=H*255;  // BOGUS
    switch(which) {
    case "R":
    case "r":
      Val=R;
      break;
    case "G":
    case "g":
      Val=G;
      break;
    case "B":
    case "b":
      Val=B;
      break;
    default:
      Val=-1;
      print ("error");
      break;
    }
    return Val;
  }
 
  for (h=0; h<1; h+=.29) {
    for (s=.13; s<1; s+=.38) {
      for (v=.22; v<1; v+=.34) {
        r=bogus_hsvtorgb(h,s,v,"r");
        g=bogus_hsvtorgb(h,s,v,"g");
        b=bogus_hsvtorgb(h,s,v,"b");
        print (h, "  ", s, "  ", v, "  ", r ,"  ", g, "  ", b);
      }
    }
  }
}
    int hsv_to_rgb(double h, double s, double v) {
        printf("h:%lf s:%lf v:%lf\n", h, s, v);
        while (h < 0) h += 360;
        while (h >= 360) h -= 360;
        int h_pos = h / 60;
        double f = h / 60 - h_pos;
        printf("h:%lf h_pos:%d f:%lf\n", h, h_pos, f);
        float p = v * (1.0 - s);
        float q = v * (1.0 - s * f);
        float t = v * (1.0 - s * (1.0 - f));
        printf("p:%lf q:%lf t:%lf\n", p, q, t);
        float r, g, b;
        
        switch (h_pos) {
            case 0: r = v; g = t; b = p; break;
            case 1: r = q; g = v; b = p; break;
            case 2: r = p; g = v; b = t; break;
            case 3: r = p; g = q; b = v; break;
            case 4: r = t; g = p; b = v; break;
            case 5: r = v; g = p; b = q; break;
            default: r = g = b = 0; break;
        }
        printf("r:%lf g:%lf b:%lf\n", r, g, b);
        int rgb_r = (int)(r * 255 + 0.5);
        int rgb_g = (int)(g * 255 + 0.5);
        int rgb_b = (int)(b * 255 + 0.5);
        printf("r:%d g:%d b:%d\n", rgb_r, rgb_g, rgb_b);
        rgb_r = rgb_r<0?0:rgb_r>255?255:rgb_r;
        rgb_g = rgb_g<0?0:rgb_g>255?255:rgb_g;
        rgb_b = rgb_b<0?0:rgb_b>255?255:rgb_b;
        printf("r:%d g:%d b:%d\n", rgb_r, rgb_g, rgb_b);
        int rgb = (rgb_r<<16) + (rgb_g<<8)+rgb_b;
        printf("rgb:%06x\n", rgb);
        return rgb;
    }


I use this method, return rgb value

By the way, does gvpr support two-dimensional arrays?

Sorry, no.
There is an ugly awk trick that may work. (see Multidimensional (The GNU Awk User’s Guide))
You concatenate the 2 (or more) subscripts with a known “special” delimiter (e.g. idx=4321 “|” “abc”) and use split to extract.
But you have to manage all the concatenating, splitting and value management.