Skip to content

added color average filtering to razer chroma keyboard code #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 116 additions & 21 deletions KeyboardVisualizerCommon/RazerChroma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
//Index lists for BlackWidow
int BlackWidowXIndex[22];
int BlackWidowYIndex[6];
int BlackWidowAvgCnt[6];
int BlackWidowAvgIndex[6*3];

//Index lists for BlackWidow TE
int BlackWidowTEXIndex[18];
int BlackWidowTEYIndex[6];
int BlackWidowTEAvgCnt[6];
int BlackWidowTEAvgIndex[6*3];

//Index lists for Blade Stealth
int BladeStealthXIndex[16];
int BladeStealthYIndex[6];
int BlackStealthAvgCnt[6];
int BlackStealthAvgIndex[6*3];

//Index list for Firefly
int FireflyIndex[15];
Expand Down Expand Up @@ -118,6 +124,89 @@ void FillKeyboardGrid(int x_count, int y_count, int * x_idx_list, int * y_idx_li
}
}

void FillKeyboardGridAvg(int x_count, int y_count, int *tmpColumnCount, int *tmpColumnColor, ChromaSDK::Keyboard::CUSTOM_EFFECT_TYPE * effect, COLORREF pixels[64][256])
{
//defines how many pixels will get averaged out per row/column
float pxCntCol = ((float)SPECTROGRAPH_COLS / (float)x_count);
float pxCntRow = ((float)SPECTROGRAPH_ROWS / (float)y_count);
//reset added up colors for next column
memset(tmpColumnCount, 0, y_count * sizeof(int));
memset(tmpColumnColor, 0, y_count * 3 * sizeof(int));
//variables for current keyboard row and column
int cRow = 0, cColumn = 0;
//temporary color storage
int cCol[3];
cCol[0] = 0; cCol[1] = 0; cCol[2] = 0;
//used to determine if keyboard row/column is done
int cRowMulti = 1; int cColumnMulti = 1;
for (int cColumnTotal = 0; cColumnTotal < SPECTROGRAPH_COLS; cColumnTotal++)
{
if (cColumnTotal >= (cColumnMulti*pxCntCol)) //one column done
{
for (int k = 0; k < y_count; k++) //iterate through rows
{
effect->Color[k][cColumn] =
RGB((((tmpColumnColor[(k * 3) + 0]) / tmpColumnCount[k]) & 0xFF),
(((tmpColumnColor[(k * 3) + 1]) / tmpColumnCount[k]) & 0xFF),
(((tmpColumnColor[(k * 3) + 2]) / tmpColumnCount[k]) & 0xFF));
}
//reset added up colors for next column
memset(tmpColumnCount, 0, y_count * sizeof(int));
memset(tmpColumnColor, 0, y_count * 3 * sizeof(int));
//increase column count
cColumnMulti++;
cColumn++;
//done handling columns
if (cColumn >= x_count)
break;
}
for (int cRowsTotal = 0; cRowsTotal < SPECTROGRAPH_ROWS; cRowsTotal++) //rows
{
if (cRowsTotal >= (cRowMulti*pxCntRow)) //one row in current column done
{
tmpColumnColor[(cRow * 3)+0] += cCol[0];
tmpColumnColor[(cRow * 3)+1] += cCol[1];
tmpColumnColor[(cRow * 3)+2] += cCol[2];
//clear colors
cCol[0] = 0; cCol[1] = 0; cCol[2] = 0;
//increase row count
cRowMulti++;
cRow++;
//done handling rows
if (cRow >= y_count)
break;
}
cCol[0] += GetRValue(pixels[ROW_IDX_SPECTROGRAPH_TOP + cRowsTotal][cColumnTotal]);
cCol[1] += GetGValue(pixels[ROW_IDX_SPECTROGRAPH_TOP + cRowsTotal][cColumnTotal]);
cCol[2] += GetBValue(pixels[ROW_IDX_SPECTROGRAPH_TOP + cRowsTotal][cColumnTotal]);
//increase total colors added for this row/column for division later
tmpColumnCount[cRow]++;
}
if (cRow < y_count) //last row was not set yet
{
//add last row
tmpColumnColor[(cRow * 3)+0] += cCol[0];
tmpColumnColor[(cRow * 3)+1] += cCol[1];
tmpColumnColor[(cRow * 3)+2] += cCol[2];
}
//clear colors
cCol[0] = 0; cCol[1] = 0; cCol[2] = 0;
//reset row count
cRowMulti = 1;
cRow = 0;
}
if (cColumn < x_count) //last column was not set yet
{
for (int k = 0; k < y_count; k++) //iterate through rows
{
effect->Color[k][cColumn] =
RGB(((tmpColumnColor[(k * 3) + 0]) / tmpColumnCount[k]),
((tmpColumnColor[(k * 3) + 1]) / tmpColumnCount[k]),
((tmpColumnColor[(k * 3) + 2]) / tmpColumnCount[k]));
}
//no color reset needed here, will be done on next call
}
}

void FillKeypadGrid(int x_count, int y_count, int * x_idx_list, int * y_idx_list, ChromaSDK::Keypad::CUSTOM_EFFECT_TYPE * effect, COLORREF pixels[64][256])
{
Expand All @@ -137,18 +226,19 @@ void RazerChroma::Initialize()
use_keyboard_generic_effect = false;
use_headset_custom_effect = false;
use_chromalink_single_color = false;
use_keyboard_nearest_filter = false;
disable_chromalink = false;
chroma_box_mode = 1;

// Dynamically loads the Chroma SDK library.
hModule = LoadLibrary(CHROMASDKDLL);
if (hModule)
{
INIT Init = (INIT)GetProcAddress(hModule, "Init");
if (Init)
{
// Dynamically loads the Chroma SDK library.
hModule = LoadLibrary(CHROMASDKDLL);
if (hModule)
{
INIT Init = (INIT)GetProcAddress(hModule, "Init");
if (Init)
{
//Initialize the SDK
Init();
Init();

//Build index list for BlackWidow
SetupKeyboardGrid(22, 6, BlackWidowXIndex, BlackWidowYIndex);
Expand Down Expand Up @@ -296,8 +386,8 @@ void RazerChroma::Initialize()
ChromaBox4BarXIndex[3][x-48] = (x * (256 / 64)) + (256 / 128);
}
}
}
}
}
}
}


Expand All @@ -324,8 +414,10 @@ bool RazerChroma::SetLEDs(COLORREF pixels[64][256])
{
//Keyboard Effect
ChromaSDK::Keyboard::CUSTOM_EFFECT_TYPE KeyboardEffect;

FillKeyboardGrid(22, 6, BlackWidowXIndex, BlackWidowYIndex, &KeyboardEffect, pixels);
if(use_keyboard_nearest_filter)
FillKeyboardGrid(22, 6, BlackWidowXIndex, BlackWidowYIndex, &KeyboardEffect, pixels);
else
FillKeyboardGridAvg(22, 6, BlackWidowAvgCnt, BlackWidowAvgIndex, &KeyboardEffect, pixels);
//Set Razer "Three Headed Snake" logo to the background color of the 11th column
KeyboardEffect.Color[0][20] = pixels[ROW_IDX_SINGLE_COLOR][11 * (256 / 22)];

Expand All @@ -335,9 +427,10 @@ bool RazerChroma::SetLEDs(COLORREF pixels[64][256])
{
//Blackwidow Chroma
ChromaSDK::Keyboard::CUSTOM_EFFECT_TYPE BlackWidowEffect;

FillKeyboardGrid(22, 6, BlackWidowXIndex, BlackWidowYIndex, &BlackWidowEffect, pixels);

if (use_keyboard_nearest_filter)
FillKeyboardGrid(22, 6, BlackWidowXIndex, BlackWidowYIndex, &BlackWidowEffect, pixels);
else
FillKeyboardGridAvg(22, 6, BlackWidowAvgCnt, BlackWidowAvgIndex, &BlackWidowEffect, pixels);
//Set Razer "Three Headed Snake" logo to the background color of the 11th column
BlackWidowEffect.Color[0][20] = pixels[ROW_IDX_SINGLE_COLOR][11 * (256 / 22)];

Expand All @@ -349,19 +442,21 @@ bool RazerChroma::SetLEDs(COLORREF pixels[64][256])

//Blackwidow Chroma Tournament Edition
ChromaSDK::Keyboard::CUSTOM_EFFECT_TYPE BlackWidowTEEffect;

FillKeyboardGrid(18, 6, BlackWidowTEXIndex, BlackWidowTEYIndex, &BlackWidowTEEffect, pixels);

if (use_keyboard_nearest_filter)
FillKeyboardGrid(18, 6, BlackWidowTEXIndex, BlackWidowTEYIndex, &BlackWidowTEEffect, pixels);
else
FillKeyboardGridAvg(18, 6, BlackWidowTEAvgCnt, BlackWidowTEAvgIndex, &BlackWidowTEEffect, pixels);
//Set Razer "Three Headed Snake" logo to the background color of the 11th column
BlackWidowTEEffect.Color[0][20] = pixels[ROW_IDX_SINGLE_COLOR][11 * (256 / 22)];

CreateEffect(ChromaSDK::BLACKWIDOW_CHROMA_TE, ChromaSDK::CHROMA_CUSTOM, &BlackWidowTEEffect, NULL);

//Blade Stealth and Blade 14
ChromaSDK::Keyboard::CUSTOM_EFFECT_TYPE BladeStealthEffect;

FillKeyboardGrid(16, 6, BladeStealthXIndex, BladeStealthYIndex, &BladeStealthEffect, pixels);

if (use_keyboard_nearest_filter)
FillKeyboardGrid(16, 6, BladeStealthXIndex, BladeStealthYIndex, &BladeStealthEffect, pixels);
else
FillKeyboardGridAvg(16, 6, BlackStealthAvgCnt, BlackStealthAvgIndex, &BladeStealthEffect, pixels);
CreateEffect(ChromaSDK::BLADE_STEALTH, ChromaSDK::CHROMA_CUSTOM, &BladeStealthEffect, NULL);
CreateEffect(ChromaSDK::BLADE, ChromaSDK::CHROMA_CUSTOM, &BladeStealthEffect, NULL);

Expand Down
1 change: 1 addition & 0 deletions KeyboardVisualizerCommon/RazerChroma.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RazerChroma
bool use_keyboard_generic_effect;
bool use_headset_custom_effect;
bool use_chromalink_single_color;
bool use_keyboard_nearest_filter;
int chroma_box_mode;
bool disable_chromalink;

Expand Down
4 changes: 4 additions & 0 deletions KeyboardVisualizerCommon/Visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ void Visualizer::SetDeviceProperty(char * devprop, char * argument)
{
rkb.use_chromalink_single_color = true;
}
else if (strcmp(devprop, "razer_use_keyboard_nearest_filter") == 0)
{
rkb.use_keyboard_nearest_filter = true;
}
else if (strcmp(devprop, "razer_disable_chromalink") == 0)
{
rkb.disable_chromalink = true;
Expand Down