BGRABitmap tutorial 4/es: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: BGRABitmap tutorial 4/es
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Esta tutoría muestra como acceder directamente a los píxeles. | |
| 2 | + | ||
| 3 | + | ### Crear un nuevo proyecto | |
| 4 | + | ||
| 5 | + | Crea un nuevo proyecto y añade referencia a [[BGRABitmap]], de la misma forma que en [[BGRABitmap tutorial/es|la primer tutoría]]. | |
| 6 | + | ||
| 7 | + | ### Añade un manejador de pintado | |
| 8 | + | ||
| 9 | + | Con el inspector de objetos, añade un manejador OnPaint y escribe : | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 13 | + | var x,y: integer; | |
| 14 | + | p: PBGRAPixel; | |
| 15 | + | image: TBGRABitmap; | |
| 16 | + | begin | |
| 17 | + | image := TBGRABitmap.Create(ClientWidth,ClientHeight); | |
| 18 | + | ||
| 19 | + | for y := 0 to image.Height-1 do | |
| 20 | + | begin | |
| 21 | + | p := image.Scanline[y]; | |
| 22 | + | for x := 0 to image.Width-1 do | |
| 23 | + | begin | |
| 24 | + | p^.red := x*256 div image.Width; | |
| 25 | + | p^.green := y*256 div image.Height; | |
| 26 | + | p^.blue := 0; | |
| 27 | + | p^.alpha := 255; | |
| 28 | + | inc(p); | |
| 29 | + | end; | |
| 30 | + | end; | |
| 31 | + | image.InvalidateBitmap; // cambiado para acceso directo | |
| 32 | + | ||
| 33 | + | image.Draw(Canvas,0,0,True); | |
| 34 | + | image.free; | |
| 35 | + | end; | |
| 36 | + | ``` | |
| 37 | + | ||
| 38 | + | Este procedimiento crea un bitmap del mismo tamaño que el disponible en el espacio del cliente. Luego el 'loop' accede directamente a los datos del píxel para renderizar un degradado bidimensional. Finalmente la imágen es dibujada y liberada. | |
| 39 | + | ||
| 40 | + | Para acceder a los datos del bitmap, también puedes usar Data, si no interesa el orden de líneas, o ScanLine para acceder a una línea específica. Con una línea, los píxeles son ordenados de izquierda a derecha. Cada componente es definido. Por ejemplo : | |
| 41 | + | ||
| 42 | + | ``` pascal | |
| 43 | + | p^.red := x*256 div image.Width; | |
| 44 | + | ``` | |
| 45 | + | ||
| 46 | + | Define un componente rojo variando de 0 a 255 de izquierda a derecha. El valor máximo de image.Width nunca es alcanzado por x, entonces el componente rojo nunca alcanza 256. | |
| 47 | + | ||
| 48 | + | ### Ejecuta el programa | |
| 49 | + | ||
| 50 | + | Deberías ver una forma con un degradado, donde las esquinas son negro, rojo, amarillo y verde. Cuando redimensionas la forma, el gradiente es redimensionado en consecuencia. | |
| 51 | + | ||
| 52 | + | <figure> | |
| 53 | + | <img src="BGRATutorial4.png" title="BGRATutorial4.png" /> | |
| 54 | + | <figcaption>BGRATutorial4.png</figcaption> | |
| 55 | + | </figure> | |
| 56 | + | ||
| 57 | + | ### Usando espacio de color HSLA | |
| 58 | + | ||
| 59 | + | Puedes usar matiz, saturación y luminosidad. Para hacer esto, declara un THSLAPixel. Su rango de valores va desde 0 0a 65535. Para convertirlo en un píxel estándar RBG, usa HSLAToBGRA. | |
| 60 | + | ||
| 61 | + | ``` pascal | |
| 62 | + | procedure TForm1.FormPaint(Sender: TObject); | |
| 63 | + | var x,y: integer; | |
| 64 | + | p: PBGRAPixel; | |
| 65 | + | image: TBGRABitmap; | |
| 66 | + | hsla: THSLAPixel; | |
| 67 | + | begin | |
| 68 | + | image := TBGRABitmap.Create(ClientWidth,ClientHeight); | |
| 69 | + | hsla.lightness := 32768; | |
| 70 | + | hsla.alpha := 65535; | |
| 71 | + | for y := 0 to image.Height-1 do | |
| 72 | + | begin | |
| 73 | + | p := image.Scanline[y]; | |
| 74 | + | hsla.saturation := y*65536 div image.Height; | |
| 75 | + | for x := 0 to image.Width-1 do | |
| 76 | + | begin | |
| 77 | + | hsla.hue := x*65536 div image.Width; | |
| 78 | + | p^:= HSLAToBGRA(hsla); | |
| 79 | + | inc(p); | |
| 80 | + | end; | |
| 81 | + | end; | |
| 82 | + | image.InvalidateBitmap; // cambiado para acceso directo | |
| 83 | + | ||
| 84 | + | image.Draw(Canvas,0,0,True); | |
| 85 | + | image.free; | |
| 86 | + | end; | |
| 87 | + | ``` | |
| 88 | + | ||
| 89 | + | <figure> | |
| 90 | + | <img src="BGRATutorial4b.png" title="BGRATutorial4b.png" /> | |
| 91 | + | <figcaption>BGRATutorial4b.png</figcaption> | |
| 92 | + | </figure> | |
| 93 | + | ||
| 94 | + | [[BGRABitmap tutorial 3/es|Tutoría anterior (dibujando con el ratón)]] \| [[BGRABitmap tutorial 5/es|Siguiente tutoría (capas y máscaras)]] | |
| 95 | + | ||
| 96 | + | --- | |
| 97 | + | *From the Free Pascal wiki, page "BGRABitmap tutorial 4/es", as it stood on 2020-02-10 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 98 | + | Category: Castellano | |
| 99 | + | Category: Español |
