Thread: POSIX C Programming sub-board?

  1. #61
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Alright then. Apologies accepted.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #62
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Wait. You called me a zealot, and I apologized? Brlblbrlblbl, you meanie.

  3. #63
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I like to just cut the middleman when apologies are in order. Makes (my) life so much easier.

    You should start doing it too. Feeling like apologizing to someone? Apologize and accept your own apology. There! Problem solved. With the added benefit you don't risk running into some arsehole who can't accept apologies. Besides, who's should always accept an apology? The person that asked of course! He's the one that knows whether his apology is sincere or not.

    Life should be this simple.
    Last edited by Mario F.; 08-22-2015 at 12:11 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #64
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Matticus View Post
    I personally do not have the desire
    to change my user title - but if I
    ever did, I would hope it would be
    something creative and colorful.


    I tried writing some code that did this color thing, I'm still working on it, but so far I have it like this:

    $>./rainbow "lyrics.txt" "#FF0000" "#00FF00" "#0000FF" "out.txt"

    Jumping up and down the floor,
    My head is an animal.
    And once there was an animal,
    It had a son that mowed the lawn.
    The son was an OK guy,
    They had a pet dragonfly.
    The dragonfly it ran away,
    But it came back with a story to say.

    Her dirty paws and furry coat,
    She ran down the forest slopes.
    The forest of talking trees,
    They used to sing about the birds and the bees.
    The bees had declared a war,
    The sky wasn't big enough for them all.
    The birds, they got help from below,
    From dirty paws and the creatures of snow.

    So for a while things were cold,
    They were scared down in their holes.
    The forest that once was green
    Was colored black by those killing machines.
    But she and her furry friends
    Took down the queen bee and her men.
    And that's how the story goes,
    The story of the beast with those four dirty paws.


    With only 2 colors ("#FF0000" "#0000FF") :


    Jumping up and down the floor,
    My head is an animal.
    And once there was an animal,
    It had a son that mowed the lawn.
    The son was an OK guy,
    They had a pet dragonfly.
    The dragonfly it ran away,
    But it came back with a story to say.

    Her dirty paws and furry coat,
    She ran down the forest slopes.
    The forest of talking trees,
    They used to sing about the birds and the bees.
    The bees had declared a war,
    The sky wasn't big enough for them all.
    The birds, they got help from below,
    From dirty paws and the creatures of snow.

    So for a while things were cold,
    They were scared down in their holes.
    The forest that once was green
    Was colored black by those killing machines.
    But she and her furry friends
    Took down the queen bee and her men.
    And that's how the story goes,
    The story of the beast with those four dirty paws.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #65
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    You'll get better color transitions if you mix the colors in YCbCr color space instead of RGB.

  6. #66
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm going to report you lot to the moderators.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #67
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Alpo View Post
    I tried writing some code that did this color thing, I'm still working on it, but so far I have it like this:
    Very nice. Another fun feature to try adding is direction of coloring.

    Left-to-right:

    Code:
    --start=left --color1=red --color2=blue
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890

    Top-to-bottom:

    Code:
    --start=top --color1=blue --color2=green
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890

    Diagonal:

    Code:
    --start=topleft --color1=FF11FF --color2=23FFFF
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890
    1234567890

  8. #68
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Matticus
    Very nice. Another fun feature to try adding is direction of coloring.
    I knew you would have the vertical as well lol! The mixture of the two is a challenge, I'll have to work it out.

    You'll get better color transitions if you mix the colors in YCbCr color space instead of RGB.
    I'll give it a shot, thanks .
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  9. #69
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,980
    I would love to see a much wider block of "1-0" text where a message is visible within the block using contrasting colors. Could the cursing filter filter it?

    gg

  10. #70
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Alpo View Post
    I knew you would have the vertical as well lol! The mixture of the two is a challenge, I'll have to work it out.
    Yeah, the diagonal took quite a bit of "pen and paper" algorithm development - good fun!

  11. #71
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Codeplug View Post
    I would love to see a much wider block of "1-0" text where a message is visible within the block using contrasting colors. Could the cursing filter filter it?

    gg
    Are you suggesting encoding cuss words into a block of colored numbers?

  12. #72
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Nominal Animal View Post
    You'll get better color transitions if you mix the colors in YCbCr color space instead of RGB.
    I figured out the problem with the transitions. I was dividing the color-step by the entire line length, rather than the length of the segment for a transition. So it wasn't fully reaching one color before changing to another.

    I thought I would post the part I had the most trouble with. Most of the rest of the program is just the Color class, which has some RegExp's in a static map to recognize the color type. This looping part gave me trouble as it was such a brain bender to me:

    Code:
    // I know it's probably golfed down too much
    
    bool Rainbow::output (std::ifstream& in, std::ofstream& out, const bool& html /*false*/) const {
        bool isOpen = in.is_open() && out.is_open();
        if (isOpen) {
            std::string line("");
    
            // Loop through line by line.
            while (std::getline(in, line, '\n')) {
    
                // Calculate max width of line segment
                const float width = line.length() / (float)(m_numColors - 1);
    
                // Get value copy of first color, in order to accumulate changes.
                // Possibly it would work to use (character_index / width) ratio as well.
                Color currentColor = **m_colors.begin();
    
                // Loop through colors from 1 to end, we need the index so not using iterators.
                for (int i = 1, iEnd = m_colors.size(); i < iEnd; i++) {
    
                    // Loop through characters of segment we are working on,
                    // substr automatically cuts off too long lines.
                    // This basically works the same as sprite animation.
                    for (const auto& ch : line.substr((int)((i - 1) * width),(int)((i - 1) * width + width))) {
    
                        // This seems necessary as colored spaces don't seem to be preserved on vBulletin
                        if (isspace(ch)) {
                            out << ch;
                            continue;
                        }
    
                        // Wrap the character in font tags, or forum specific tags with the color value
                        out << htmlWrap(currentColor, ch, html);
    
                        // Update the color by taking difference of where we are and where we
                        // are going, then divide by the length in units of a line segment.
                        // Again this works like moving sprites by velocity.
                        currentColor += ((*m_colors[i] - *m_colors[i - 1]) / width);
                    }
                    // Update the current color to the next one, again by value
                    currentColor = *m_colors[i];
                }
                out << (html ? "<BR>" : "\r\n");
            }
        }
        return isOpen;
    }
    Sample output with multiple colors :

    $> ./rainbow "test.txt""#FF0000" "#00FFFF" "#FF00FF"

    This establishment's name is Hochberghaus. It is in Bohemia, a short day's
    journey from Vienna, and being in the Austrian Empire is of course a health resort.
    The empire is made up of health resorts; it distributes health to the whole world.
    Its waters are all medicinal. They are bottled and sent throughout the earth; the
    natives themselves drink beer. This is self-sacrifice apparently--but
    outlanders who have drunk Vienna beer have another idea about it.
    Last edited by Alpo; 08-24-2015 at 07:52 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  13. #73
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Matticus View Post
    Yeah, the diagonal took quite a bit of "pen and paper" algorithm development - good fun!
    Isn't it just normal bilinear interpolation,
    V = ( (h - y) ⋅ ( (w - x) ⋅ V00 + x ⋅ V0w ) / w + y ⋅ ( (w - x) ⋅ Vh0 + x ⋅ Vhw ) / w ) / h
    per component? V00 being the value at x=0,y=0, V0w at x=w,y=0, Vh0 at x=0,y=h, and Vhw at x=w,y=h?

    Ah, no; you probably want the start and end values for each line, i.e.
    V0 = ( (h - y) ⋅ V00 + y ⋅ Vh0 ) / h
    Vw = ( (h - y) ⋅ V0w + y ⋅ Vhw ) / h
    and then interpolate the color on each line, as the number of characters w on each line varies, ie.
    V = ( (w - x)⋅ V0 + x ⋅ Vw ) / w

    If you only have the two components for the diagonal, the other two can be either mid way,
    V = (V0 + V1) / 2
    or scaled by the aspect ratio,
    V0w = (w⋅V00 + h⋅Vhw) / (w + h)
    Vh0 = (h⋅V00 + w⋅Vhw) / (w + h)
    depending on whether you want the perpendicular line of the gradient to also be diagonal, or just at straight angles to the defined diagonal. It only shows up when the text block is non-square, though.

    Also, like Mario implied in #66, these are ANSI, not POSIX.1, if you do them in a terminal.

  14. #74
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Nominal Animal View Post
    Isn't it just normal bilinear interpolation
    Actually, my approach is probably a lot more basic than that. It only works with two colors, and steps the color components of each successive diagonal linearly from the start color to the end color.

    The equations you posted seem much more advanced than the implementation in my own version of the program.

    To illustrate the basic approach I used with a simple example, let's assume the following:

    • Use letters in lieu of color codes
    • The start letter is 'A'
    • The end letter is 'U'
    • The number of rows is 4
    • The number of columns is 8


    This would give us a step size of:

    ('U' - 'A') / (8-1) + (4-1) = 20 / 10 = 2

    Printed out, it would look like this:

    Code:
    A C E G I K M O
    C E G I K M O Q
    E G I K M O Q S
    G I K M O Q S U
    Or, to illustrate it with a diagram:

    POSIX C Programming sub-board?-diagonal-png

    To me, this general approach was the best way to achieve simple diagonal coloring. (As this was just intended as a quick "fun" project, I tried to avoid getting too fancy.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX programming in Windows?
    By jw232 in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2009, 09:03 AM
  2. Replies: 6
    Last Post: 03-12-2008, 01:19 PM
  3. Posix Thread Programming
    By reddybk1 in forum C Programming
    Replies: 8
    Last Post: 10-14-2004, 04:51 PM
  4. Posix programming
    By reddybk1 in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2004, 03:48 PM
  5. Posix Thread programming
    By reddybk1 in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2004, 11:40 PM