Php imagecolorallocate

Source: Internet
Author: User
Tags color identifier

Php imagecolorallocate

Imagecolorallocate-assign a color image

Int imagecolorallocate (resource $ image, int $ red, int $ green, int $ blue

)

Returns the color of a given RGB component.

Imagecolorallocate () must be called to create each color, which will be used in the image represented by the image.

Note: the first time imagecolorallocate () is called to fill the image of the color palette background-image creation make

Use imagecreate ().

Image
Image resources are created through image functions such as imagecreatetruecolor ().

Red
The value of the red component.

Green
The green component of value.

Blue
The value of the blue component.

These parameters are between 0 and 255, or 0x00 and 0xFF hexadecimal integers.

Report error return value
A-color identifier or FALSE if the allocation fails.


<? Php

$ Im = imagecreate (100,100 );

// Sets background to red
$ Background = imagecolorallocate ($ im, 255, 0, 0 );

// Sets some colors
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );

// Hexadecimal way
$ White = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );
$ Black = imagecolorallocate ($ im, 0x00, 0x00, 0x00 );

?>
There is a very simple function, basically the same imagecolorallocate (), without the need for only one

Image resource work.

<? Php
Function createcolor ($ r, $ g, $ B ){
Return hexdec (str_pad (dechex ($ r), 2, 0, STR_PAD_LEFT). str_pad (dechex ($ g ),

2, 0, STR_PAD_LEFT). str_pad (dechex ($ B), 2, 0, STR_PAD_LEFT ));
}

/* As mentioned above, this function is not exactly the same thing as imagecolorallocate (), instead

An image resource is required. This means that the code in the following two blocks has identical results :*/

$ Color = colorcreate (105,199,204 );

// Block 2
$ Img = imagecreatetruecolor (100,100); // the arguments here don't really

Matter
$ Color = imagecolorallocate ($ img, 105,199,204 );
Imagedestroy ($ img );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.