don't click here

Need help with converting RGB to YUV

Discussion in 'Technical Discussion' started by RetroKoH, Nov 12, 2012.

  1. RetroKoH

    RetroKoH

    Member
    1,662
    22
    18
    Project Sonic 8x16
    So... One of the things I'm implementing in Sonic Triad's Palette Editor is the ability to convert RGB to YUV.

    Basically, I want to be able to apply this type of effect on palettes... seen in this image taken from wikipedia.org:
    [​IMG]


    The black and white one is its Y component... which is a variation of greyscaling. The blue/green is its U component. The last is the V component.

    Now, I've got equations that I've been told should do the trick... Now, I believe the first is working as it should be, as RGB are set to the value. But I am applying the equations for U and V to all three RGB values as well... and I know that's why the color doesn't come across correctly, but I don't know how to configure the values to the colors correctly.

    Here is my code from Triad's open source for all 3 YUV functions. PLEASE NOTE that this is all in GML, as Triad is written using Game Maker.
    Code (Text):
    1.  
    2.  
    3.         // Y - Equations based on what I found on www.fourcc.org/fccyvrgb.php
    4.         if (yuvmode==1) {for(I=c1;I!=c2;I+=c3)
    5.         {
    6.             m[I,a]=((color_get_red(pcol[I,a])*0.299)+(color_get_green(pcol[I,a])*0.587)+(color_get_blue(pcol[I,a])*0.114)); // Weighted Average of the 3 color components for each color.
    7.             c[I,a]=round(m[I,a]/34)*34; // The colors are made Gens compatible, as colors are multiples of 34. ($22)
    8.             // Get our final color code, in format 0BGR
    9.             pal[I,a]="0"+string_char_at(dec_to_hex(c[I,a]),2)
    10.                         +string_char_at(dec_to_hex(c[I,a]),2)
    11.                         +string_char_at(dec_to_hex(c[I,a]),2);                                        
    12.             pcol[I,a]=draw_hex_color(pal[I,a]); // Set drawing color for palette box.
    13.         }}
    14.  
    15.         // U
    16.         if (yuvmode==2) {for(I=c1;I!=c2;I+=c3)
    17.         {
    18.             m[I,a]=((color_get_red(pcol[I,a])*0.147)-(color_get_green(pcol[I,a])*0.289)-(color_get_blue(pcol[I,a])*0.436)+128); // Weighted Average of the 3 color components for each color.
    19.             c[I,a]=round(m[I,a]/34)*34; // The colors are made Gens compatible, as colors are multiples of 34. ($22)
    20.             // Get our final color.
    21.             pal[I,a]="0"+string_char_at(dec_to_hex(c[I,a]),2)
    22.                         +string_char_at(dec_to_hex(c[I,a]),2)
    23.                         +string_char_at(dec_to_hex(c[I,a]),2);                                        
    24.             pcol[I,a]=draw_hex_color(pal[I,a]); // Set drawing color for palette box.
    25.         }}
    26.  
    27.         // V
    28.         if (yuvmode==3) {for(I=c1;I!=c2;I+=c3)
    29.         {
    30.             m[I,a]=((color_get_red(pcol[I,a])*0.615)-(color_get_green(pcol[I,a])*0.515)-(color_get_blue(pcol[I,a])*0.1)+128); // Weighted Average of the 3 color components for each color.
    31.             c[I,a]=round(m[I,a]/34)*34; // The colors are made Gens compatible, as colors are multiples of 34. ($22)
    32.             // Get our final color.
    33.             pal[I,a]="0"+string_char_at(dec_to_hex(c[I,a]),2)
    34.                         +string_char_at(dec_to_hex(c[I,a]),2)
    35.                         +string_char_at(dec_to_hex(c[I,a]),2);                                        
    36.             pcol[I,a]=draw_hex_color(pal[I,a]); // Set drawing color for palette box.
    37.         }}
    38.  
    All three apply the same value to all 3 colors, resulting in contrasting forms of Greyscaling, which I DON'T WANT! I know THAT is why my effect is not working correctly... but I don't know what exactly to do to fix this. Any help or suggestions? If this isn't clear enough I can try to elaborate more but am in a slight hurry to get the question out at the moment.
     
  2. Vangar

    Vangar

    Member
    3,654
    62
    28
    I'm confused on why you need to do that? Is it going to create colour effects or something?
     
  3. GerbilSoft

    GerbilSoft

    RickRotate'd. Administrator
    2,971
    76
    28
    USA
    rom-properties
    The "effect" shown on Wikipedia is used to illustrate what the different YUV components are. In reality, you're not going to be able to get that sort of effect on the MD due to limited color depth, and there's really no point in doing that anyway. It'll just result in a horribly undersaturated image that isn't very usable.
     
  4. RetroKoH

    RetroKoH

    Member
    1,662
    22
    18
    Project Sonic 8x16
    Yea... It was supposed to be for a color effect. I actually ended up getting this to work somewhat yesterday, though not 100% accurately... And though it wasn't exact, I can't say I really cared too much for the result...

    I was gonna keep tweaking it, but I guess I can just scrap it then... Oh we'll... Was worth a try though. I guess, back to the gradient builder I go then.