Type: | Package |
Title: | Create Colour Palettes from Images |
Version: | 0.1.1 |
Author: | Alan Inglis |
Maintainer: | Alan Inglis <alan.n.inglis@gmail.com> |
Description: | Can take in images in either .jpg, .jpeg, or .png format and creates a colour palette of the most frequent colours used in the image. Also provides some custom colour palettes. |
License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
Encoding: | UTF-8 |
Imports: | ggplot2, dplyr, jpeg, png, grDevices, httr, stats, pixmap |
RoxygenNote: | 7.2.3 |
Suggests: | knitr, rmarkdown, scales |
VignetteBuilder: | knitr |
NeedsCompilation: | no |
Packaged: | 2023-09-26 13:41:24 UTC; alaninglis |
Repository: | CRAN |
Date/Publication: | 2023-09-26 14:00:02 UTC |
avgHex
Description
Average Hex Colors per Group. This function takes a data frame with two columns: one for the hex color values and another for the group labels. It calculates the average color for each group and returns a data frame with the group labels and their corresponding average hex colors.
Usage
avgHex(df, hex_col, group_col)
Arguments
df |
A data frame with columns for hex color values and group labels. |
hex_col |
The name of the column containing hex color values. Default is "hex". |
group_col |
The name of the column containing group labels. Default is "group". |
Value
A data frame with the group labels and their corresponding average hex colors.
Examples
df <- data.frame(
hex = c("#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF"),
group = c(1, 1, 2, 2, 3)
)
avgHex(df, hex_col = "hex", group_col = "group")
colPalette
Description
Create a Color Palette. This function generates a custom color palette based on the specified 'palette' name. The color palettes are sourced from two predefined lists: 'taylor_palettes' and 'radiohead_palettes'.
Usage
colPalette(palette)
Arguments
palette |
A character string specifying the name of the colour palette. The palette must be present in either 'taylor_palettes' or 'radiohead_palettes'. |
Value
A colorRampPalette function with the specified colour palette.
Examples
my_palette <- colPalette(palette = "evermore")
colours <- my_palette(5)
print(colors)
excludeCols
Description
A non-exhaustive list of white and black colour shades. For use when setting exclude = TRUE
in the getTopCol
function. Setting exclude = TRUE
when calling the getTopCol
function will exclude the colours form the results.
Usage
excludeCols(data, customExclude = NULL)
Arguments
data |
Data frame of colours |
customExclude |
Character vector. Optional vector of custom color codes in HEX format to be excluded. |
Value
No return value, called for side effects.
getExtension
Description
Get the file extension of a given file. This function takes a file path as input and returns the file extension without the dot.
Usage
getExtension(file)
Arguments
file |
A character string containing the file path. |
Value
A character vector containing the file extension without the dot.
Examples
getExtension("example.txt")
getTopCol
Description
Get top colors from an image. This function reads an image file, extracts the colors, and returns the top n colors based on their frequency in the image. Optionally, black and white shades can be excluded, and the colors can be grouped and averaged.
Usage
getTopCol(
path,
n = NULL,
exclude = TRUE,
sig = 4,
avgCols = TRUE,
n_clusters = 5,
customExclude = NULL
)
Arguments
path |
Character, the path to the image file (either jpg or png). |
n |
Integer, the number of top colors to return. If NULL (default), return all colors. |
exclude |
Logical, whether to exclude black and white shades. Default is TRUE. |
sig |
Integer, the number of decimal places for the color percentage. Default is 4. |
avgCols |
Logical, whether to average the colors by groups. Default is TRUE. |
n_clusters |
Integer, the number of clusters to use for grouping colors. Default is 5. |
customExclude |
Character vector. Optional vector of custom color codes in HEX format to be excluded. |
Value
A data frame with the top colors, their frequency, and percentage in the image.
groupCols
Description
Group Hex Colors. This function takes a vector of hex color values and groups them using k-means clustering in the RGB color space. It returns a data frame with the original hex colors and their assigned group labels.
Usage
groupCols(hex_colors, n_clusters = 5)
Arguments
hex_colors |
A character vector of hex color values. |
n_clusters |
The number of clusters (groups) to use in the k-means clustering. Default is 5. |
Value
A data frame with the original hex colors and their assigned group labels.
Examples
hex_colors <- c("#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF")
groupCols(hex_colors, n_clusters = 2)
Create a Colour Palette from an Image
Description
This function generates a colour palette based on the most prominent colors in an image..
Usage
img2pal(
path,
n = NULL,
avgCols = FALSE,
exclude = FALSE,
n_clusters = NULL,
customExclude = NULL
)
Arguments
path |
Character string. The path to the image file. |
n |
Integer. The number of most frequent colours in input image to consider. |
avgCols |
Logical. Whether to average colours within clusters. |
exclude |
Character vector. List of colours in HEX format to exclude from the palette. |
n_clusters |
Integer. The number of clusters for colour grouping. |
customExclude |
Character vector. Optional vector of custom color codes in HEX format to be excluded. |
Value
A colour palette generated from input image.
Examples
pal<-img2pal(path = "https://raw.githubusercontent.com/AlanInglis/colouR/master/images/bender.png",
n = 10,
avgCols = TRUE,
exclude = TRUE,
n_clusters = 15,
customExclude = NULL)
pal
plotPalette
Description
Plot a color palette from a data frame. This function takes a vector of colours or a data frame with a column of colors and plots the colors as a color palette.
Usage
plotPalette(df, color_col)
Arguments
df |
A vector of colours or a data frame containing a column with colors in hexadecimal format. |
color_col |
A character string representing the name of the column containing the colors. |
Value
A ggplot2 plot object displaying the color palette.
Examples
# Create a sample data frame with colors
colors_df <- data.frame(
color = c("#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF")
)
# Plot the color palette with default bar height
plotPalette(colors_df, "color")
list of radiohead colour palettes
Description
list of radiohead colour palettes
Usage
radiohead_palettes
Format
An object of class list
of length 9.
read_image_from_url
Description
Read either a PNG or JPG file from a URL. This function reads an image file (PNG or JPG) from a URL and returns the image data.
Usage
read_image_from_url(path)
Arguments
path |
A character string representing the URL of the image file. |
Value
An object containing the image data. If the image is a JPG, the object will be of class "array". If the image is a PNG, the object will be of class "matrix".
scaleColor
Description
Create a Custom Color Scale for ggplot2. This function creates a custom color scale for ggplot2 plots based on the specified 'palette' name. The color palettes are sourced from two predefined lists: 'taylor_palettes' and 'radiohead_palettes'. The function can create either a discrete or continuous color scale, depending on the 'discrete' parameter. Additional arguments passed to the function are forwarded to the corresponding ggplot2 scale function.
Usage
scaleColor(palette = "pabloHoney", discrete = TRUE, ...)
Arguments
palette |
A character string specifying the name of the color palette. The palette must be present in either 'taylor_palettes' or 'radiohead_palettes'. |
discrete |
A logical value indicating whether to create a discrete (TRUE) or continuous (FALSE) color scale. Default is TRUE. |
... |
Additional arguments to be passed to the ggplot2 scale function. |
Value
A ggplot2 colour scale based on the specified color palette.
scaleFill
Description
Create a Custom Fill Scale for ggplot2. This function creates a custom fill scale for ggplot2 plots based on the specified 'palette' name. The color palettes are sourced from two predefined lists: 'taylor_palettes' and 'radiohead_palettes'. The function can create either a discrete or continuous fill scale, depending on the 'discrete' parameter. Additional arguments passed to the function are forwarded to the corresponding ggplot2 scale function.
Usage
scaleFill(palette, discrete = TRUE, ...)
Arguments
palette |
A character string specifying the name of the color palette. The palette must be present in either 'taylor_palettes' or 'radiohead_palettes'. |
discrete |
A logical value indicating whether to create a discrete (TRUE) or continuous (FALSE) fill scale. Default is TRUE. |
... |
Additional arguments to be passed to the ggplot2 scale function. |
Value
A ggplot2 fill scale based on the specified color palette.
list of Taylor swift colour palettes
Description
list of Taylor swift colour palettes
Usage
taylor_palettes
Format
An object of class list
of length 10.