Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Hexadecimal

Comments in 'Plugin Development' started by MyNameIsTriXz, Aug 21, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    As far as I know the compoundTag "customColor" (for setting custom colors on leather armor) only accepts hexadecimal values:
    PHP:
    $color 0x0064ff00;
    As you can see it doesnt have those ""...
    My question:
    I want to apply an hex color to the hexadecimal color, which is in string format.
    PHP:
    $color 0x00."64ff00"
    Does not work, so how can I apply a string like "ffffff" to a hexadecimal variable?
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Are you sure it only accepts only hex values? Because they all have the same values, just the format is different across different numbering systems (16 is 16 in decimal (base-10), 10 in hexadecimal (base-16), 20 in octal (base-8), 00010000 in binary (base-2), etc.). And why not just convert the specified hex value altogether rather than appending it to 0x00 (0)? You can use hexdec(), or intval() with the base parameter set to 16.
    PHP:
    echo 0x00."64ff00"//doesn't output 6618880, instead outputs 064ff00
    echo 0x0064ff00//outputs 6618880
    echo hexdec("64ff00"); //outputs 6618880
    echo intval("64ff00"16); //also outputs 6618880, profit!
    Last edited: Aug 21, 2016
    MyNameIsTriXz and SOFe like this.
  3. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Almost all numbers in almost all programming languages are internally in binary form. Extremely few are in decimal. Even much less are in other bases.
    However, to most programmers for modern languages, you don't need to think about what base the number is. When you type 0x10, or when you type 020, or when you type 0b10000, or when you type 16, they all resolve into the same number, which is internally (in the processor) expressed in binary form, 10000 (actually many more zeroes before that, to pad the number to the int size) (actually zeroes behind as well, because many processors prefer computation in little endian rather than big endian).

    customColor is just a normal number (an int). To the client's screen, this number doesn't make sense unless it is expressed in base 256. because it only cares the first digit (alpha), the second digit (amount of red light), the third digit (amount of green light), and the four digit (amount of blue light). To the networking handlers, this number doesn't make sense unless it is expressed in base 2, because the networking handlers only know to send data in bits. To a programmer, this number doesn't make sense unless it is expressed in base 16, because programmers instinctively know what color it is whenever they see the hexadecimal expression of color codes.

    The above clarifies that 0x64ff00, 0x0064ff00, or 6618880 have no difference to the machine. It won't remember which form it is supplied from. It just stores the number in binary form, that's all. And you want to set the customColor attribute to the number 6618880, by parsing a string in base 16 into a number.

    Actually, the answer is as simple as one function: intval(). It will parse a string into a number, using the base specified in the second parameter, or 10 by default. You pass "64ff00" to intval(), and it will return a number that is the value of the number 64FF00 [16].

    So the code for this question is:
    PHP:
    intval("64FF00"16)
    You can parse the string "0x64FF00" directly as well, telling PHP to take the "0x" into account to specify that the base is 16, by passing the base of 0:
    PHP:
    intval("0x64FF00"0)
    Base 0 is the placeholder base for "detect base automatically".
    Gamecrafter and MyNameIsTriXz like this.
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Correction: First digit = red, second digit = green, third digit = blue, fourth digit = alpha. Just curious, between intval() and hexdec(), would there be any differences performance-wise?
  5. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    It actually depends. Some formats use ARGB for the sake of compatibility with RGB ints. Some formats use RGBA.
    In the context of colored leather armor, according to Minecraft Wiki, the color is a simple pure RGB, bits 1-7 ignored (default 0) and bit 0 (signum) always 0 (if 1 will set to white).

Share This Page

Advertisement