Skip to content
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

Apply colors based on distance from atom 1 #723

Merged
merged 1 commit into from
Aug 12, 2021
Merged
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
32 changes: 32 additions & 0 deletions avogadro/qtplugins/applycolors/applycolors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ ApplyColors::ApplyColors(QObject* parent_)
connect(action, SIGNAL(triggered()), SLOT(applyIndexColors()));
m_actions.append(action);

action = new QAction(tr("By Distance"), this);
action->setData(atomColors);
connect(action, SIGNAL(triggered()), SLOT(applyDistanceColors()));
m_actions.append(action);

action = new QAction(tr("By Element"), this);
action->setData(atomColors);
connect(action, SIGNAL(triggered()), SLOT(resetColors()));
Expand Down Expand Up @@ -164,6 +169,33 @@ void ApplyColors::applyIndexColors()
m_molecule->emitChanged(QtGui::Molecule::Atoms);
}

void ApplyColors::applyDistanceColors()
{
if (m_molecule == nullptr && m_molecule->atomCount() == 0)
return;

bool isSelection = !m_molecule->isSelectionEmpty();
Vector3 firstPos = m_molecule->atomPosition3d(0);
Real size = 2.0 * m_molecule->radius();

// probably better to get color scales, but for now do it manually
auto numAtoms = m_molecule->atomCount();
for (Index i = 0; i < numAtoms; ++i) {
// if there's a selection and this atom isn't selected, skip it
if (isSelection && !m_molecule->atomSelected(i))
continue;

Vector3 currPos = m_molecule->atomPosition3d(i);
Vector3 diff = currPos - firstPos;
Real distance = diff.norm();
Real distanceFraction = distance / size;

m_molecule->atom(i).setColor(rainbowGradient(distanceFraction));
}

m_molecule->emitChanged(QtGui::Molecule::Atoms);
}

void ApplyColors::resetColors()
{
if (m_molecule == nullptr)
Expand Down
1 change: 1 addition & 0 deletions avogadro/qtplugins/applycolors/applycolors.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public slots:
private slots:
void openColorDialog();
void applyCustomColor(const QColor& color);
void applyDistanceColors();
void applyIndexColors();
void resetColors();

Expand Down