Objective-C Method Parameter Parameters

I am trying to define an extremely simple utility method that will save me from having to use a calculator to determine RGB values ​​in percent. When I look into a sample Apple code called "QuartzCache" in the file DrawView.m, line 96, I see this:

float whiteColor[4] = {1, 1, 1, 1};

However, when I try to create a method like the following, the compiler hates me. Half an hour of intense googling did not help.

 +(float[])percentagesRGBArray:(float[])rgbArray{

        float red = rgbArray[0];
        float green = rgbArray[1];
        float blue = rgbArray[2];
        float alpha = rgbArray[3];
        red = red/255;
        green = green/255;
        blue = blue/255;
        alpha = alpha;
        float percentagesRGBArray[4] = {red, green, blue, alpha};


        return  percentagesRGBArray;
    }

What is the correct way to define such a method? What am I doing wrong here?

+3
source share
4 answers

struct, , NSNumber. NSColor, .

  • struct :

    typedef struct
    {
        float red;
        float green;
        float blue;
        float alpha;
    } MyColor;
    
    - (MyColor) percentagesRGBArray:(MyColor) incoming
    {
        MyColor result;
        result.red = incoming.red / 255;
        result.green = incoming.green / 255;
        result.blue = incoming.blue / 255;
        result.alpha = incoming.alpha;
        return result;
    }
    
  • NSNumber :

    - (NSArray *) percentagesRGBArray:(float[]) rgbArray
    {
        NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
        NSNumber *green = [NSNumber numberWithFloat:rgbArray[1] / 255];
        NSNumber *blue = [NSNumber numberWithFloat:rgbArray[2] / 255];
        NSNumber *alpha = [NSNumber numberWithFloat:rgbArray[3]];
    
        return [NSArray arrayWithObjects:red, green, blue, alpha, nil];
    }
    
  • NSColor :

    - (NSColor *) percentagesRGBArray:(float[]) rgbArray
    {
        CGFloat red = rgbArray[0] / 255;
        CGFloat green = rgbArray[1] / 255;
        CGFloat blue = rgbArray[2] / 255;
        CGFloat alpha = rgbArray[3];
    
        return [NSColor colorWithDeviceRed:red
                                     green:green
                                      blue:blue
                                     alpha:alpha];
    }
    
+5

Cocoa NSColor , , - .

:

typedef struct
{
    float red;
    float green;
    float blue;
    float alpha;
}
RGBAData;

RGBAData ConvertRGBAToPercentages(const RGBAData source)
{
    RGBAData percentages;

    percentages.red = source.red/255;
    percentages.green = source.green/255;
    percentages.blue = source.blue/255;
    percentages.alpha = source.alpha/255;

    return percentages;
}

:

RGBAData original = { 0xFF, 0xFF, 0x00, 0x80 }; // 50% transparent yellow
RGBAData percents = ConvertRGBAToPercentages(original);
+3

e.James dreamlax . , :

, , C. . , , . - float myArray[4] ( ), , 4 , . . ? , , . , , . , malloc , .

C-, . . , API, .

+2
source

I think I'm late), but I just found this thread.

C way to do this is to create an array before calling the function;

+(void) percentagesRGBArray:(float[])inArray toArray:(float*)outArray {
    ...
} 

float array1[4];
float array2[4];
[MyClass percentagesRGBArray:array1 toArray:array2];
0
source

Source: https://habr.com/ru/post/1735320/


All Articles