Title: | Create Colour Palettes from Images |
---|---|
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. |
Authors: | Alan Inglis |
Maintainer: | Alan Inglis <[email protected]> |
License: | GPL (>=2) |
Version: | 0.1.1 |
Built: | 2025-03-04 05:49:12 UTC |
Source: | https://github.com/alaninglis/colour |
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.
avgHex(df, hex_col, group_col)
avgHex(df, hex_col, group_col)
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". |
A data frame with the group labels and their corresponding average hex colors.
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")
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")
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'.
colPalette(palette)
colPalette(palette)
palette |
A character string specifying the name of the colour palette. The palette must be present in either 'taylor_palettes' or 'radiohead_palettes'. |
A colorRampPalette function with the specified colour palette.
my_palette <- colPalette(palette = "evermore") colours <- my_palette(5) print(colors)
my_palette <- colPalette(palette = "evermore") colours <- my_palette(5) print(colors)
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.
excludeCols(data, customExclude = NULL)
excludeCols(data, customExclude = NULL)
data |
Data frame of colours |
customExclude |
Character vector. Optional vector of custom color codes in HEX format to be excluded. |
No return value, called for side effects.
Get the file extension of a given file. This function takes a file path as input and returns the file extension without the dot.
getExtension(file)
getExtension(file)
file |
A character string containing the file path. |
A character vector containing the file extension without the dot.
getExtension("example.txt")
getExtension("example.txt")
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.
getTopCol( path, n = NULL, exclude = TRUE, sig = 4, avgCols = TRUE, n_clusters = 5, customExclude = NULL )
getTopCol( path, n = NULL, exclude = TRUE, sig = 4, avgCols = TRUE, n_clusters = 5, customExclude = NULL )
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. |
A data frame with the top colors, their frequency, and percentage in the image.
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.
groupCols(hex_colors, n_clusters = 5)
groupCols(hex_colors, n_clusters = 5)
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. |
A data frame with the original hex colors and their assigned group labels.
hex_colors <- c("#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF") groupCols(hex_colors, n_clusters = 2)
hex_colors <- c("#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF") groupCols(hex_colors, n_clusters = 2)
This function generates a colour palette based on the most prominent colors in an image..
img2pal( path, n = NULL, avgCols = FALSE, exclude = FALSE, n_clusters = NULL, customExclude = NULL )
img2pal( path, n = NULL, avgCols = FALSE, exclude = FALSE, n_clusters = NULL, customExclude = NULL )
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. |
A colour palette generated from input image.
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
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
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.
plotPalette(df, color_col)
plotPalette(df, color_col)
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. |
A ggplot2 plot object displaying the color palette.
# 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")
# 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
radiohead_palettes
radiohead_palettes
An object of class list
of length 9.
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.
read_image_from_url(path)
read_image_from_url(path)
path |
A character string representing the URL of the image file. |
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".
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.
scaleColor(palette = "pabloHoney", discrete = TRUE, ...)
scaleColor(palette = "pabloHoney", discrete = TRUE, ...)
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. |
A ggplot2 colour scale based on the specified color palette.
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.
scaleFill(palette, discrete = TRUE, ...)
scaleFill(palette, discrete = TRUE, ...)
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. |
A ggplot2 fill scale based on the specified color palette.
list of Taylor swift colour palettes
taylor_palettes
taylor_palettes
An object of class list
of length 10.