CheerLights-API

 view release on metacpan or  search on metacpan

CheerLights/API.pm  view on Meta::CPAN

    return unless $response;

    my $data = decode_json($response);
    my @feeds = @{$data->{feeds}};
    my @history;

    foreach my $entry (@feeds) {
        push @history, {
            color => $entry->{field1},
            hex => $entry->{field2},
            timestamp => $entry->{created_at}
        };
    }

    return \@history;
}

sub color_name_to_hex {
    my ($color_name) = @_;
    my %color_map = (
        red       => '#FF0000',
        green     => '#00FF00',
        blue      => '#0000FF',
        cyan      => '#00FFFF',
        white     => '#FFFFFF',
        warmwhite => '#FDF5E6',
        purple    => '#800080',
        magenta   => '#FF00FF',
        yellow    => '#FFFF00',
        orange    => '#FFA500',
        pink      => '#FFC0CB',
        oldlace   => '#FDF5E6',
    );

    return $color_map{lc $color_name};
}

sub hex_to_rgb {
    my ($hex_code) = @_;
    $hex_code =~ s/^#//;  # Remove the leading '#'
    return map { hex($_) } ($hex_code =~ /(..)(..)(..)/);
}

sub is_valid_color {
    my ($color_name) = @_;
    my @valid_colors = qw(
        red green blue cyan white warmwhite
        purple magenta yellow orange pink oldlace
    );

    return grep { $_ eq lc $color_name } @valid_colors;
}

# Private function to fetch the content from a URL
sub _fetch_url {
    my ($url) = @_;
    my $ua = LWP::UserAgent->new;
    my $response = $ua->get($url);

    if ($response->is_success) {
        return $response->decoded_content;
    } else {
        warn "Failed to fetch URL: " . $response->status_line;
        return;
    }
}

1;  # Ensure the module returns a true value

__END__

=head1 NAME

CheerLights::API - A Perl module for accessing the CheerLights API.

=head1 SYNOPSIS

  use CheerLights::API qw(get_current_color get_current_hex get_color_history);

  my $current_color = get_current_color();
  print "Current color: $current_color->{color}, Hex: $current_color->{hex}\n";

  my $hex = get_current_hex();
  print "Current hex color: $hex\n";

  my $history = get_color_history(5);
  foreach my $entry (@$history) {
      print "Color: $entry->{color}, Hex: $entry->{hex}, Timestamp: $entry->{timestamp}\n";
  }

=head1 DESCRIPTION

This module provides functions for interacting with the CheerLights API, allowing you to get the current color, its history, and more.

=head1 FUNCTIONS

=head2 get_current_color

=head2 get_current_hex

=head2 get_current_color_name

=head2 get_color_history

=head2 color_name_to_hex

=head2 hex_to_rgb

=head2 is_valid_color

=head1 AUTHOR

Hans Scharler <hans@nothans.com>

=head1 COPYRIGHT AND LICENSE

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut



( run in 0.966 second using v1.01-cache-2.11-cpan-df04353d9ac )