[PHP] Converting Armagetron Colors to HTML Colors

Everything todo with programming goes HERE.
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

[PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

I'm not sure entirely if this is going to be useful because this simply converts your color coded string from armagetron into the html format.

It also might crash sometimes if the color codes are not found in the string.

Anyway, I hope people would find it useful :)

UPDATE
Moves sources to launchpad and updated the script with version 2.0.
Updated link: Launchpad - arma_colors_to_html_codes

Website link to test the script out is here: Vertrex - Armagetron Colors to Html Codes
Last edited by LOVER$BOY on Mon Dec 02, 2013 3:43 am, edited 4 times in total.
Image
nux
Round Winner
Posts: 206
Joined: Mon Sep 12, 2011 11:20 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by nux »

  • Both colors are in hexadecimal value, they are not RGB in arma, all that changes is the prefix (# in html, 0x in arma). RBG colors are in decimal value (0-255, 0-255, 0-255); im speaking about HTML/CSS, but this applies in other cases as well.
  • Remove the else at line 95, not the whole block, just the else. It will display the form even if theres post data, so you dont have to refill.
  • Dont spam your script with comments, make your code (more) readable instead. Stuff like "if ($firstTime == true) // if this is the first time finding a color code" or "$strPos = false; //now turn this back to it's original value, false" is unnecessary, and a little bit stupid.
  • Have a soft and/or hard limit on characters per line, will make your code easier to read. Something around 60-80 for soft and 100-120 for hard should work wonders.
  • Check out the html standards and start using them as soon as possible. Always write valid html. I believe the <font> tag is deprecated and shouldnt be used; use <span style="color:$color"> instead.
  • Tables should be used to display tabular data, not elements like input. If you want them to be on the next line, either use <br> or set them as display:block
  • A textarea is an element in which the user can feed data to the server, not the other way around. Use div or p instead.
##################################################################
###### The reason I did $firstTime so that when the color ######
###### code is at 0, the stupid while loop doesn't think ######
###### that it's false (as many would know, flase = 0) ######
##################################################################
That is why you use === instead of ==, 0 == false, but 0 !== false. See the warning in the Return Values in the man page for strpos() in http://php.net/strpos

Code: Select all

$colorCode = str_replace("0x", "#", $colorCode);			// replace 0x with #
This is the wrong way to do it, you want to generate a color from this, and then replace the entire block on a copy, not the original. Displaying the original will be useful if whoever is used your script is just trying stuff.

Code: Select all

				$new_strPos = $strPos + 7;		// set the new length to get
Dont assume every 0x is meant for coloring. I sometimes use 0x6e as name, which is not a color. You certainly want to use regex in here, you need a 0x followed by 6 hexadecimal numbers, im a little rusty on PCRE, but it should be something like #0x[0-9a-f]{6}#i.

Code: Select all

	$source_string = $_POST["text"];
	$new_string = '';

	if ($source_string != "")
	{
		$strPos = strpos($source_string, "0x");		// get the first point of the color code
		$strPos++;									// increment it so that if the first code is at the beginning, the script won't fail
		$new_strPos = 0;							// the length the substr would be taking from $source_string
		$firstTime = true;							// this is going to be the first time the color code is found
This is a really really bad habit, do not use camelCase and underscore variables at the same time; pick one and stick with it.

With all these things ive pointed its hard to say "that aside, good work" without knowing your PHP background, this would have taken me 3 hours 5 years ago and would take me 3 minutes today (after i get some sleep).
There's a difference between knowing your shit, and knowing you're shit. Grammar does matter.
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

lol.

Good to see someone is awake to point out that I don't have good php background. No. This is rough and not that good (you said it yourself). I only know php enough to get by.

If it's not useful, oh well. I might as well use it for myself. No problem :P

BTW nux, <font></font> hasn't been depreciated yet :P

Oh also, I use a debugger (VS 2010) to do all the major parts for me. It's actually a good thing the serial was fixed a few days ago. Otherwise, I wouldn't have been able to get this much done with normal coding alone :P

And ... this is one thing I'm going to be open and straight. Everything I do is too complex for people to understand. nux I don't blame you if you don't understand but I feel that with the current technology, I want "self_control". That's why I made so many comments. Besides, I checked the code and it worked for me on my browser. Yes. If people are using that type like you, then they better watch out for themselves. IF they are going to use it like that, at-least they could change that 0 to a o. After converting it, then you can change it back to 0. Simply and effective. If your going to forget that small thing, what's the point in using the converter in the first place?

Look nux. I'm going to be straight to the matter. I will not stop writing comments. Why should I change because you called it "stupid". Do you really understand the reason for those comments? Well! Do you?!
idk but I'm going to tell anyway. Those comments are these for the people who want to code but that haven't actually started to properly learn. I fit into that category. Do you understand the hours and hours I spent on trying to understand a very simple script? Now I called it simple but back then I didn't understand it one bit.

My teacher always told me repeatedly "Leave comments boy. That way, the one who can read your script will be able to understand it." I'm sure you understand that what he meant was "I may understand what im coding but does the other person does? What if the code is sooo complex that they give up trying to understand it?" That scared me. I'm a free guy who does things for me (even when they said they'll pay me).

Enough with the unnecessary talk. I'm out and straight. I'm not extremely good at php but I do it because it's fun to code something new (that you might find boring). Not aimed at you. My mother, Software engineer, is enjoying the coding she does and so do I! :D I love it! Because I'm not studying programming properly, I can't do the things you are telling me to change.
Last edited by LOVER$BOY on Mon Aug 27, 2012 12:54 pm, edited 1 time in total.
Image
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by Jonathan »

LOVER$BOY wrote:BTW nux, <font></font> hasn't been depreciated yet :P
Oh yes. The font tag has been deprecated since 1999 or so. It's gone in XHTML 1.0 Strict, XHTML 1.1, and HTML5.
LOVER$BOY wrote:Oh nux. Many of us who want to do coding aren't as sophisticated as us to simply read the code and understand it. My IT teacher always aid "Comments help understand". As his quite made sense to me, I thought that I might as well, no matter how stupid it might be, start writting for the sake of the less able people.
Comments aren't meant to mirror the code. Yes, I can tell you're setting this to false. Saying that is akin to shrinking a car for use by toddlers, who would wreck it anyway if they managed to drive off. Comments should be complementary, to highlight that which is hard to discover reading the code. Code should be as self-documenting as possible by structuring it well and picking sensible names for things. If that fails to show what's going on, add comments. Even be slightly liberal adding them. But adding them willy-nilly just creates noise, making it harder to pick out the comments that really matter.
ˌɑrməˈɡɛˌtrɑn
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

Jonathan wrote:
LOVER$BOY wrote:BTW nux, <font></font> hasn't been depreciated yet :P
Oh yes. The font tag has been deprecated since 1999 or so. It's gone in XHTML 1.0 Strict, XHTML 1.1, and HTML5.
Then explain to me why is it still being used in html?
Jonathan wrote:
LOVER$BOY wrote:Oh nux. Many of us who want to do coding aren't as sophisticated as us to simply read the code and understand it. My IT teacher always aid "Comments help understand". As his quite made sense to me, I thought that I might as well, no matter how stupid it might be, start writting for the sake of the less able people.
Comments aren't meant to mirror the code. Yes, I can tell you're setting this to false. Saying that is akin to shrinking a car for use by toddlers, who would wreck it anyway if they managed to drive off. Comments should be complementary, to highlight that which is hard to discover reading the code. Code should be as self-documenting as possible by structuring it well and picking sensible names for things. If that fails to show what's going on, add comments. Even be slightly liberal adding them. But adding them willy-nilly just creates noise, making it harder to pick out the comments that really matter.
So, basically, your happy as long as you can read and understand?
Image
User avatar
Jip
Round Winner
Posts: 397
Joined: Sat Sep 26, 2009 5:32 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by Jip »

Code: Select all

<?php
function convertArmaColor( $string, $resett = 'ffffff' )
{
    $pattern = array(
        '/0x([0-9a-fA-F]{6})([^0x]*)/'
        ,'/(0xRESETT)([^0x]*)/'
    );
    $replace = array(
        '<span style="color:#\1;">\2</span>'
        ,'<span style="color:#' . $resett . ';">\2</span>'
    );
    return preg_replace( $pattern, $replace, $string );
}

print convertArmaColor( '0xff1122test 0x99aa00another test 0xRESETTresett test.' );

?>
:)
Last edited by Jip on Mon Aug 27, 2012 2:07 pm, edited 2 times in total.
User avatar
Jonathan
A Brave Victim
Posts: 3391
Joined: Thu Feb 03, 2005 12:50 am
Location: Not really lurking anymore

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by Jonathan »

LOVER$BOY wrote:Then explain to me why is it still being used in html?
Because they live in the previous millennium and use the corresponding old HTML version? Explain to me why there are still a few flat-earthers around.
LOVER$BOY wrote:So, basically, your happy as long as you can read and understand?
That's the point, isn't it?

You'll often see overly verbose comments in tutorial code, to help people understand the language. But if it's holding you back with real code, you're doing it wrong. If you don't understand your tools, you can't do much, and indeed that kind of code tends to be riddled with mistakes. Don't get me wrong. The comments are not the cause, just another symptom of a novice programmer. If you just stop commenting on obvious things it won't magically get better, but do consider how you could improve to get there (where you'll also get more of a clue of what you're doing, besides manipulating low-level statements).

Jip: considered there may be more than one color code, or that uppercase doesn't count? And I'll throw 0xRESETT into the discussion, too.
ˌɑrməˈɡɛˌtrɑn
User avatar
Jip
Round Winner
Posts: 397
Joined: Sat Sep 26, 2009 5:32 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by Jip »

Jonathan wrote:Jip: considered there may be more than one color code, or that uppercase doesn't count? And I'll throw 0xRESETT into the discussion, too.
Fixed.
epsy
Adjust Outside Corner Grinder
Posts: 2003
Joined: Tue Nov 07, 2006 6:02 pm
Location: paris
Contact:

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by epsy »

Well, here's my javascript+jQuery version I used in serverlist.js

Code: Select all

function text_el(text)
{
    return jQuery(document.createTextNode(text));
}

// Parses color codes; Used right about everywhere.
function pcc(text, eltype)
{
    var children = false;
    if( eltype === undefined )
    {
        eltype = 'span';
        children = true;
    }
    var ret = jQuery(document.createElement(eltype));
    if( serverlist.filter_colors )
    {
        ret.append(fcc(text));
        return ret;
    }
    var first = true;
    var last_i = 0;
    text.replace(
        /0x(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|RESETT|.{6})(.*?)(?=0x(?:.{6}|RESETT)|$)/g,
        function(sub, hex, r_, g_, b_, colored, i){
            if( first && i )
            {
                ret.append(text_el(text.slice(0, i)));
            }
            first = false;
            var el;
            if(hex == 'RESETT' || !r_ || !g_ || !b_)
            {
                el = text_el(colored);
            }
            else
            {
                var el = jQuery('<span class="color-code" style="color: #' + hex + '">');
                var r, g, b;
                r = parseInt(r_, 16);
                g = parseInt(g_, 16);
                b = parseInt(b_, 16);
                if(is_dark(r, g, b))
                {
                    el.addClass('dark');
                }
                el.text(colored);
            }
            ret.append(el);
            last_i = i + sub.length;
            return sub;
        });
    ret.append(text_el(text.slice(last_i)))
    if( children )
        return jQuery(ret).children();
    return jQuery(ret);
}

Code: Select all

pcc(data.raw.name, 'strong')
nux
Round Winner
Posts: 206
Joined: Mon Sep 12, 2011 11:20 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by nux »

Everything i pointed out, i did from a mentoring point of view. If you are not able to take constructive criticism, your code will suck, or if you are good enough, it will take you longer to realise there is a better way.
LOVER$BOY wrote:And ... this is one thing I'm going to be open and straight. Everything I do is too complex for people to understand. nux I don't blame you if you don't understand but I feel that with the current technology, I want "self_control". That's why I made so many comments. Besides, I checked the code and it worked for me on my browser.
What do you mean exactly with everything you do it too complex? Have you tried making it simpler? Maybe you think its complex when in reality it isnt. Whatever the reason, you dont get "self_control" (whatever the hell that means) from over-commenting your code.
LOVER$BOY wrote:Yes. If people are using that type like you, then they better watch out for themselves. IF they are going to use it like that, at-least they could change that 0 to a o. After converting it, then you can change it back to 0. Simply and effective. If your going to forget that small thing, what's the point in using the converter in the first place?
Always assume the users are going to feed badly formatted data to your script, its unintentional most of the times, so handling this properly is YOUR job, not theirs. Account for the idiots.
LOVER$BOY wrote:Look nux. I'm going to be straight to the matter. I will not stop writing comments. Why should I change because you called it "stupid". Do you really understand the reason for those comments? Well! Do you?!
idk but I'm going to tell anyway. Those comments are these for the people who want to code but that haven't actually started to properly learn. I fit into that category. Do you understand the hours and hours I spent on trying to understand a very simple script? Now I called it simple but back then I didn't understand it one bit.
This is why we cant have nice things. Newbs should not learn from other newbs, it is really really stupid. This is how bad habits propagate. Same goes for the other way, do not code for newbs, dont try to teach, because you will most likely be wrong and they will think it is ok to do those things.
LOVER$BOY wrote:My teacher always told me repeatedly "Leave comments boy. That way, the one who can read your script will be able to understand it." I'm sure you understand that what he meant was "I may understand what im coding but does the other person does? What if the code is sooo complex that they give up trying to understand it?" That scared me. I'm a free guy who does things for me (even when they said they'll pay me).
There is a balance you have to find yourself, no comments is as bad as too many comments. Just check out other people's scripts, not simple ones like this one, but really complex.
LOVER$BOY wrote:Enough with the unnecessary talk. I'm out and straight. I'm not extremely good at php but I do it because it's fun to code something new (that you might find boring). Not aimed at you. My mother, Software engineer, is enjoying the coding she does and so do I! :D I love it! Because I'm not studying programming properly, I can't do the things you are telling me to change.
My mom says im special :).
There's a difference between knowing your shit, and knowing you're shit. Grammar does matter.
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

Great guys! epsy and Jip. Thanks for improving on my version ;)

Hey I'm not blaming you or anything nux. It's just that my mum and dad always say I make things more complicated than they already are :P That's what I meant. Even my teachers are saying that about me. Like in my 10th grade, when my teacher was coming around to collect our papers (coloured and white paper). He indicated that I put the coloured in the round box while the white in the square box. So when he came to me while I just finished, when I looked at the boxes, he reverse them which caused me to cross my arms to put the sheets in. It was hilariously funny at the time :D
nux wrote:This is why we cant have nice things. Newbs should not learn from other newbs, it is really really stupid. This is how bad habits propagate. Same goes for the other way, do not code for newbs, dont try to teach, because you will most likely be wrong and they will think it is ok to do those things.
Well that's the thing. You may say that Newbs shouldn't learn from other newbs but what can you expect? They are desperate to learn. I don't blame them. Without a proper instructor, they tend to do something they find easier to achieve wihile we don't. In this case, I found my way easier but for you it seemed rather silly. Oh well... :(
nux wrote:What do you mean exactly with everything you do it too complex? Have you tried making it simpler? Maybe you think its complex when in reality it isnt. Whatever the reason, you dont get "self_control" (whatever the hell that means) from over-commenting your code.
You see, when I meant "self_control", what I was trying to say what that I wanted to be able to control most of the variables within the code so as not to lose an idea of what's happening in it. As you might have/not understood, I have short term memories. So sometimes I tend to forget what I'm doing quickly than other times (a childhood accident that nearly killed me and the same for my dad though he slipped :P )
nux wrote:There is a balance you have to find yourself, no comments is as bad as too many comments. Just check out other people's scripts, not simple ones like this one, but really complex.
BUT nux, whenever I checked out the login scripts or the smf php scripts, they have so much comments, you think they are wasting their time on it? :P
nux wrote:Everything i pointed out, i did from a mentoring point of view. If you are not able to take constructive criticism, your code will suck, or if you are good enough, it will take you longer to realise there is a better way.
Hey, I appreciate you for it. It shows me that I got to improve but right now I can't. Studying full time with 4 subjects while keeping up with tron news and doing work is no easy joke :P It's actually a lucky coincidence that I can at-least fit my time into coding something.
nux wrote:Always assume the users are going to feed badly formatted data to your script, its unintentional most of the times, so handling this properly is YOUR job, not theirs. Account for the idiots.
Ya. I know but as I'm not exactly good at making it good, it's up to the users themselves to be careful.
Jonathan wrote:You'll often see overly verbose comments in tutorial code, to help people understand the language. But if it's holding you back with real code, you're doing it wrong. If you don't understand your tools, you can't do much, and indeed that kind of code tends to be riddled with mistakes. Don't get me wrong. The comments are not the cause, just another symptom of a novice programmer. If you just stop commenting on obvious things it won't magically get better, but do consider how you could improve to get there (where you'll also get more of a clue of what you're doing, besides manipulating low-level statements).
Can you call the comments that SMF guys write on their scripts "annoying"? :P I found them helpful when I was trying to link up my sql and the script of login from myportal.

Oh nux, this is one thing I'm going to share with you. I studied in programming at highschool from grade 11 to grade 12. In grade 12, I started learning VB and seriously it was such a headache but on the other hand, I was loving it. So imagine my shock of discovering two years later when I went back to my old source codes/ I had no clue of what I did. The comments I left on some of them made sense to me while others... simply no clue at all. It probably had something to do with my short term memory or something to do with the lack of memory strength I have. I know my parents keep complaining I can't remember things for too long but the doctor said I suffered a severe head injury so they aren't saying it to my face at-least (but I can hear them :P ). In any case, from that point I thought commenting might be a good idea.
Image
User avatar
compguygene
Adjust Outside Corner Grinder
Posts: 2342
Joined: Thu Aug 21, 2008 12:09 pm
Location: Cleveland, Ohio
Contact:

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by compguygene »

For the sake of full disclosure, I have to say that I mostly just tinker with code anymore, but I had a few years of college way back in the day (the '80s) and do know a little about programming.

Loverboy, I really think you are missing the real point here, so let me re-iterate the highlights in as kind a manner as possible.

In regards to Self Documenting Code the idea is simple. By choosing variable names, classes, methods, etc. that are self explanatory, you can make code that is very readable and understandable. Really, none of us can make the case for this nearly as well as the experts that are cited in the article that I have linked you too.
However, I do agree that some comments are necessary, if only so that you and others can understand what the code is meant to do.
30+ PHP Best Practices for Beginners wrote:10. Comment, Comment, Comment
Aside from using white space and indentations to separate the code, you’ll also want to use inline comments to annotate your code. You’ll thank yourself later when you’re needing to go back and find something in the code, or if you just can’t remember what a certain function did. It’s also useful for anyone else who needs to look over your code.
Full Article here

So, perhaps a balance between the two approaches would be the best. When I looked up what some Professional IT managers are saying, they talk about the need to adjust the way programmer's work is measured and how programmers are paid to properly motivate them to comment their code to make it more maintainable.

Personally, the best scripts and code I have seen around here has been both self documenting and containing comments. Although not the best example, check out Z-man's scripts on Launchpad. They are some bash scripts that seem to be quite understandable.
Armagetron: It's a video game that people should just play and enjoy :)
https://bit.ly/2KBGYjvCheck out the simple site about TheServerPharm
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

Ok now I clearly understand. Many thanks for simplifying things for me comp :)

Besides, I doubt I would be a fully fledged programmer. My real intention is to teach maths. Programming is merely a hobby for me right now :)
Image
User avatar
compguygene
Adjust Outside Corner Grinder
Posts: 2342
Joined: Thu Aug 21, 2008 12:09 pm
Location: Cleveland, Ohio
Contact:

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by compguygene »

I completely understand. I completed 90% of a programming degree before I realized I did not want to be a programmer, so I just got a job in the real world doing networking and pre-sales support. However, even if you are gonna be a hobby programmer, you should still employ the concepts and ideals that the pros do. It will save you a lot of trouble in the long run. I too only do it as a hobby, but I try my best to take the extra time to do it right so I don't have to completely redo stuff.
Armagetron: It's a video game that people should just play and enjoy :)
https://bit.ly/2KBGYjvCheck out the simple site about TheServerPharm
User avatar
LOVER$BOY
Match Winner
Posts: 731
Joined: Thu Jan 24, 2008 12:46 pm

Re: [PHP] Converting Armagetron Colors to HTML Colors

Post by LOVER$BOY »

haha but you see comp, you almost completed 90% where as I originally began my journey in VB and that was almost the basics. So I have trouble leaving behind some of my habits from VB coding :P

Well... when I'm teaching other people, I'll tell them to avoid the things I am doing right now. Tell them to refer to the manual more frequently and pose questions in the online forums (not here ofc) where the more advanced people could help them out :)
Image
Post Reply