File: color.pl

package info (click to toggle)
libffi-platypus-perl 2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,860 kB
  • sloc: perl: 7,388; ansic: 6,862; cpp: 53; sh: 19; makefile: 14
file content (45 lines) | stat: -rw-r--r-- 836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use strict;
use warnings;
use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new(
  api => 2,
  lib => './color.so'
);

package Color {

  use FFI::Platypus::Record;
  use overload
    '""' => sub { shift->as_string },
    bool => sub { 1 }, fallback => 1;

  record_layout_1($ffi,
    'string(8)' => 'name', qw(
    uint8     red
    uint8     green
    uint8     blue
  ));

  sub as_string {
    my($self) = @_;
    sprintf "%s: [red:%02x green:%02x blue:%02x]",
      $self->name, $self->red, $self->green, $self->blue;
  }

}

$ffi->type('record(Color)' => 'color_t');
$ffi->attach( color_increase_red => ['color_t','uint8'] => 'color_t' );

my $gray = Color->new(
  name  => 'gray',
  red   => 0xDC,
  green => 0xDC,
  blue  => 0xDC,
);

my $slightly_red = color_increase_red($gray, 20);

print "$gray\n";
print "$slightly_red\n";