diff --git a/Changes b/Changes new file mode 100644 index 0000000..fa35b5d --- /dev/null +++ b/Changes @@ -0,0 +1,2 @@ +1.00 Wed Nov 5 2024 + - Initial release to CPAN. \ No newline at end of file diff --git a/CheerLights/API.pm b/CheerLights/API.pm new file mode 100644 index 0000000..4a267f4 --- /dev/null +++ b/CheerLights/API.pm @@ -0,0 +1,171 @@ +package CheerLights::API; + +use strict; +use warnings; +use LWP::UserAgent; +use JSON; +use Exporter 'import'; + +our @EXPORT_OK = qw( + get_current_color + get_current_hex + get_current_color_name + get_color_history + color_name_to_hex + hex_to_rgb + is_valid_color +); + +our $VERSION = '1.00'; + +my $CHEERLIGHTS_FEED_URL = "https://api.thingspeak.com/channels/1417/feed.json"; + +sub get_current_color { + my $url = "$CHEERLIGHTS_FEED_URL?results=1"; + my $response = _fetch_url($url); + return unless $response; + + my $data = decode_json($response); + my $color_name = $data->{feeds}[0]{field1}; + my $hex_code = $data->{feeds}[0]{field2}; + + return { + color => $color_name, + hex => $hex_code + }; +} + +sub get_current_hex { + my $color_data = get_current_color(); + return $color_data ? $color_data->{hex} : undef; +} + +sub get_current_color_name { + my $color_data = get_current_color(); + return $color_data ? $color_data->{color} : undef; +} + +sub get_color_history { + my ($count) = @_; + $count ||= 10; # Default count to 10 if not provided + my $url = "$CHEERLIGHTS_FEED_URL?results=$count"; + my $response = _fetch_url($url); + 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 + +=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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7592310 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Hans Scharler + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..321dd29 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,8 @@ +Changes +CheerLights/API.pm +LICENSE +Makefile.PL +MANIFEST This list of files +README.md +META.yml Module YAML meta-data (added by MakeMaker) +META.json Module JSON meta-data (added by MakeMaker) diff --git a/META.json b/META.json new file mode 100644 index 0000000..5608e32 --- /dev/null +++ b/META.json @@ -0,0 +1,43 @@ +{ + "abstract" : "A Perl module for accessing the CheerLights API", + "author" : [ + "Hans Scharler " + ], + "dynamic_config" : 1, + "generated_by" : "ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010", + "license" : [ + "unknown" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "CheerLights-API", + "no_index" : { + "directory" : [ + "t", + "inc" + ] + }, + "prereqs" : { + "build" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "0" + } + }, + "runtime" : { + "requires" : { + "JSON" : "0", + "LWP::UserAgent" : "0" + } + } + }, + "release_status" : "stable", + "version" : "1.00", + "x_serialization_backend" : "JSON::PP version 4.16" +} diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..8c1cc45 --- /dev/null +++ b/META.yml @@ -0,0 +1,24 @@ +--- +abstract: 'A Perl module for accessing the CheerLights API' +author: + - 'Hans Scharler ' +build_requires: + ExtUtils::MakeMaker: '0' +configure_requires: + ExtUtils::MakeMaker: '0' +dynamic_config: 1 +generated_by: 'ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010' +license: unknown +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: CheerLights-API +no_index: + directory: + - t + - inc +requires: + JSON: '0' + LWP::UserAgent: '0' +version: '1.00' +x_serialization_backend: 'CPAN::Meta::YAML version 0.018' diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..25a9d3d --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,12 @@ +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'CheerLights::API', + VERSION_FROM => 'CheerLights/API.pm', + AUTHOR => 'Hans Scharler ', + ABSTRACT => 'A Perl module for accessing the CheerLights API', + PREREQ_PM => { + 'LWP::UserAgent' => 0, + 'JSON' => 0, + }, +); diff --git a/README.md b/README.md new file mode 100644 index 0000000..01e30a3 --- /dev/null +++ b/README.md @@ -0,0 +1,112 @@ + +# CheerLights::API + +**CheerLights::API** is a Perl module that provides an interface to the [CheerLights](https://cheerlights.com) API. It allows you to retrieve the current color, color history, and other useful functions related to CheerLights, a project that synchronizes connected lights globally based on social media interactions. + +🌍 🎨 💡 + +## Features + +- **Get the current CheerLights color** with its name and hex code. +- **Retrieve the history** of recent colors. +- **Convert color names to hex codes**. +- **Convert hex codes to RGB tuples**. +- **Check if a color name is valid** in the CheerLights context. + +``` + .-=-. .-=-. .-=-. + _( )__ _( )__ _( )__ + (_________)(_________)(_________) + | | | | | | + | | | | | | +-----' '------' '------' '----- +``` + +## Installation + +1. Copy the `CheerLights/API.pm` file to your desired module directory (`lib/`). +2. Use the module in your Perl script by including `use CheerLights::API`. + +## Usage + +### Import the Module + +```perl +use CheerLights::API qw(get_current_color get_current_hex get_color_history color_name_to_hex hex_to_rgb is_valid_color); +``` + +### Examples + +**Get the Current Color** +```perl +my $current_color = get_current_color(); +print "Current color: $current_color->{color}, Hex: $current_color->{hex}\n"; +``` + +**Get the Current Hex Code** +```perl +my $hex = get_current_hex(); +print "Current hex color: $hex\n"; +``` + +**Get the Color History** +```perl +my $history = get_color_history(5); +foreach my $entry (@$history) { + print "Color: $entry->{color}, Hex: $entry->{hex}, Timestamp: $entry->{timestamp}\n"; +} +``` + +**Convert a Color Name to Hex** +```perl +my $hex_code = color_name_to_hex('red'); +print "Hex code for red: $hex_code\n"; +``` + +**Convert a Hex Code to RGB** +```perl +my @rgb = hex_to_rgb('#FF0000'); +print "RGB values for #FF0000: @rgb\n"; +``` + +**Check if a Color Name is Valid** +```perl +my $is_valid = is_valid_color('blue'); +print "Is 'blue' a valid color? ", $is_valid ? 'Yes' : 'No', "\n"; +``` + +## Functions + +### `get_current_color()` +Returns the current CheerLights color as a hash reference with `color` and `hex` keys. + +### `get_current_hex()` +Returns the current hex code of the CheerLights color. + +### `get_current_color_name()` +Returns the current color name of the CheerLights. + +### `get_color_history($count)` +Takes an integer `$count` (default is 10) and returns an array reference of the most recent color data, each containing `color`, `hex`, and `timestamp`. + +### `color_name_to_hex($color_name)` +Converts a given color name to its corresponding hex code. + +### `hex_to_rgb($hex_code)` +Converts a hex code to an RGB tuple. + +### `is_valid_color($color_name)` +Checks if a color name is valid according to CheerLights' list. + +## Requirements + +- **Perl 5.10+** +- **Modules**: + - `LWP::UserAgent` + - `JSON` + +Install required modules using CPAN: + +```bash +cpan LWP::UserAgent JSON +``` \ No newline at end of file